简体   繁体   中英

Working with multiple subs in VBA

I cant seem to get this code to display the date where the stock at WalTech exceeded the searched price. (The dates are listed in column A and the prices are listed in column b) Right now the code that is giving me a problem is the message box. Can someone help me?

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

看起来您需要在上一个For循环中使用nStock而不是nStocks

You must pass the searchPass variable between subs

In sub Records type:

Call RecordHigh1(searchPrice)

In Sub RecordHigh1 type:

Sub RecordHigh1(searchPrice As Currency)

other than that your sub has many other flaws, both of syntactic and logical type

here follows a version with less possible modifications from your initial code:

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

here's a more concise and optimized (and commented) version

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM