2016-06-02 10 views
0

WalTechの株価が検索された価格を上回った日付をこのコードで表示することはできません。 (日付は列Aに表示され、価格は列bに表示されます)今すぐ問題を引き起こすコードはメッセージボックスです。誰か助けてくれますか?VBAで複数のサブと作業する

Sub Records() 
Dim searchPrice As Currency 


'Get a price a call other sub 
searchPrice = InputBox("Enter a Price") 

Call RecordHigh1 


End Sub 

Sub RecordHigh1() 

Dim stocks() As String 
Dim dt() As String 
Dim nStock As Integer 


Dim i As Integer 


'capture the stocks and dates and put them in two seperate arrays 
With wsData.Range("A2") 
    nStock = Range(.Offset(1, 0), .End(xlDown)).Rows.Count 
    ReDim stocks(1 To nStock) 
    ReDim dt(1 To nStock) 
    For i = 1 To nStock 
     stocks(i) = .Offset(i, 0).Value 
     dt(i) = .Offset(i, 1).Value 
    Next 
End With 

'Loop through arrays to find date where stock exceeds searchPrice 
With Range("A2") 
For i = 1 To nStocks 
    If .Offset(i, 1).Value > searchPrice Then 
     MsgBox "The first date WalTech stock price exceeded " & stocks(i).Value & " was " & dt(i).Value 
    Else 
     MsgBox "WalTech stock has not exceeded this price" 
    End If 
Next 
End With 


End Sub 

答えて

1

あなたは、サブレコードに潜水艦

間searchPass変数を渡す必要がありますが入力します。Sub RecordHigh1タイプで

Call RecordHigh1(searchPrice) 

:あなたのサブは、他の多くの欠陥を持っていること以外の

Sub RecordHigh1(searchPrice As Currency) 

を、構文と論理の両方のタイプ

ここでは、leあなたの最初のコードからSS可能な修正:

Sub Records() 
    Dim searchPrice As Currency 

    'Get a price a call other sub 
    searchPrice = InputBox("Enter a Price") 

    Call RecordHigh1(searchPrice)'<~~ pass the sub the variable with the price to search for 
End Sub 


Sub RecordHigh1(searchPrice As Currency)'<~~ have the Sub accept a parameter of a currency type with which make comparisons 

    Dim stocks() As String 
    Dim dt() As String 
    Dim nStock As Long '<~~ always better use Long type instead of integer 
    Dim i As Long '<~~ always better use Long type instead of integer 

    'capture the stocks and dates and put them in two seperate arrays 

    'With wsData.Range("A2")'<~~ wsData is not defined. or is it a Public variable 
    With ActiveSheet.Range("A3") '<~~ start from "A3" if your data begin from there 
     nStock = .Range(.Cells, .End(xlDown)).Rows.Count 
     ReDim stocks(1 To nStock) 
     ReDim dt(1 To nStock) 
     For i = 1 To nStock 
      stocks(i) = .Offset(i - 1, 0).Value '<~~ use i-1 to offset from the range first cell 
      dt(i) = .Offset(i - 1, 1).Value '<~~ use i-1 to offset from the range first cell 
     Next 
    End With 

    'Loop through arrays to find date where stock exceeds searchPrice 
    Dim priceExceeded As Boolean 
    With ActiveSheet.Range("A2") 
     For i = 1 To nStock 
      If .Offset(i, 1).Value > searchPrice Then '<~~ at the first occurrence of a price higher then the one passed as the limit... 
       priceExceeded = True '<~~ ...then mark you found it... 
       Exit For '<~~ ... end exit loop 
      End If 
     Next 
    End With 
    If priceExceeded Then '<~~ if the occurrence of a price higher then the one passed has been marked... 
     MsgBox "The first date WalTech stock price exceeded " & searchPrice & " was " & stocks(i) & " with " & dt(i) '<~~ ...then say it 
    Else 
     MsgBox "WalTech stock has not exceeded" & searchPrice '<~~ ...otherwise say there wasn't any 
    End If 

End Sub 

ここで、より簡潔で最適化された(とコメント)バージョンこれに加えて

Sub Records() 
    Dim searchPrice As Currency 

    'Get a price a call other sub 
    searchPrice = InputBox("Enter a Price") 

    Call RecordHigh1(searchPrice) '<~~ pass the sub the variable with the price to search for 
End Sub 


Sub RecordHigh1(searchPrice As Currency) '<~~ have the Sub accept a parameter of a currency type with which make comparisons 

    Dim stocks As Variant, dt As Variant '<~~ declare arrays as variant to exploit the possibility of filling them up with ranges 
    Dim i As Long '<~~ always better use Long type instead of integer 

    'capture the stocks and dates and put them in two seperate arrays 

    'With wsData.Range("A2")'<~~ wsData is not defined. or is it a Public variable 
    With ActiveSheet.Range("A3") '<~~ start from "A3" if your data begin from there 
     stocks = Application.Transpose(.Range(.Cells, .End(xlDown))) '<~~ fill stocks array in a single statement 
     dt = Application.Transpose(.Range(.Cells, .End(xlDown)).Offset(, 1)) '<~~ fill dt array in a single statement 
    End With 

    'Loop through arrays to find date where stock exceeds searchPrice 
    Dim priceExceeded As Boolean 
    For i = 1 To UBound(stocks) 
     If dt(i) > searchPrice Then '<~~ at the first occurrence of a price higher then the one passed as the limit... 
      priceExceeded = True '<~~ ...then mark you found it... 
      Exit For '<~~ ... end exit loop 
     End If 
    Next 
    If priceExceeded Then '<~~ if the occurrence of a price higher then the one passed has been marked... 
     MsgBox "The first date WalTech stock price exceeded " & searchPrice & " was " & Format(stocks(i), "dd/mm/yyyy") & " with " & dt(i) '<~~ ...then say it. since dates are numbers, you must format them to appear in a date format 
    Else 
     MsgBox "WalTech stock has not exceeded" & searchPrice '<~~ ...otherwise say there wasn't any 
    End If 

End Sub 
+0

私は疲れているときにこのようなことに取り組まないことを最善と思います。私が間違った間違いをさせるようにする – th65

+0

あなたは歓迎です – user3598756

1

あなたの最後のForループの代わりに、nStocksnStockを使用する必要があるように見えます。

+1

だが、すべての潜水艦の外で「明示的なオプション」を使用し、このコードは、宣言されていない変数に対してコンパイラエラーをスローします。これは、このようなスペルミスを見つけるのに役立ちます。 – OpiesDad

+0

@OpiesDadはキャッチしてくれてありがとう。私はこれを修正してサブを走らせると、searchPriceを超えるものとは対照的に、それぞれの日付を表示し始める正しいメッセージをまだ表示していません。 – th65

+0

わかりませんが、CCurを試してみてください。 Offset(i、1).Value)> searchPrice Then ' –

関連する問題