使用VBA中的多个潜在客户

时间:2016-06-02 03:42:13

标签: arrays vba excel-vba excel

我似乎无法使用此代码显示WalTech的股票超过搜索价格的日期。 (日期列在A列中,价格列在b列中)现在,给我一个问题的代码就是消息框。有人能帮助我吗?

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

2 个答案:

答案 0 :(得分:1)

您似乎需要在上一个nStock循环中使用For而不是nStocks

答案 1 :(得分:1)

您必须在subs

之间传递searchPass变量

在子记录中输入:

Call RecordHigh1(searchPrice)

在Sub RecordHigh1中输入:

Sub RecordHigh1(searchPrice As Currency)

除了你的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 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