あなたは、サブレコードに潜水艦
間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
私は疲れているときにこのようなことに取り組まないことを最善と思います。私が間違った間違いをさせるようにする – th65
あなたは歓迎です – user3598756