【发布时间】:2023-04-08 05:11:01
【问题描述】:
这是我到目前为止的代码。我创建了一个空白股票代码数组,然后创建了一个名为tickerIndex 的新变量,并使用ticker(tickerIndex) 来定位数据中的特定股票代码。我使用该特定数据点将所有交易量相加,并找到该股票的起止价格并找到回报。但是当我运行它时,它只显示标题:(。
Sub AllStockAnalysis()
Worksheets("All Stocks Analysis").Activate
tickerIndex = 0
totalVolume = 0
Dim ticker() As String
Dim startTime As Single
Dim endTime As Single
Dim yearValue As String
Dim endingPrice As Double
Dim startingPrice As Double
RowCount = Cells(Rows.Count, "A").End(xlUp).Row
yearValue = InputBox("What year would you like to run the analysis on?")
startTime = Timer
Sheets(yearValue).Activate
For i = 2 To RowCount
If Cells(i + 1, 1).Value <> Cells(i, 1).Value Or Cells(i - 1, 1).Value <> Cells(i, 1).Value Then
Cells(i, 1).Value = ticker(tickerIndex)
End If
If Cells(i, 1).Value = ticker(tickerIndex) Then
totalVolume(tickerIndex) = totalVolume(tickerIndex) + Cells(i, 8).Value
End If
If Cells(i - 1, 1).Value <> ticker(tickerIndex) And Cells(i, 1).Value = ticker(tickerIndex) Then
startingPrice = Cells(i, 3).Value
End If
If Cells(i + 1, 1).Value <> ticker(tickerIndex) And Cells(i, 1).Value = ticker(tickerIndex) Then
endingPrice = Cells(i, 6)
Sheets("All Stocks Analysis").Cells(i + 2, "A").Value = ticker(tickerIndex)
Sheets("All Stocks Analysis").Cells(i + 2, "B").Value = totalVolume(tickerIndex)
Sheets("All Stocks Analysis").Cells(i + 2, "C").Value = endingPrice / startingPrice - 1
totalVolume = 0
tickerIndex = tickerIndex + 1
End If
Next i
Worksheets("All Stocks Analysis").Activate
Range("A1").Value = "All Stocks (" + yearValue + ")"
Cells(3, 1).Value = "Ticker"
Cells(3, 2).Value = "Total Daily Volume"
Cells(3, 3).Value = "Return"
endTime = Timer
MsgBox "This code ran in " & (endTime - startTime) & " seconds for the year " & (yearValue)
End Sub
【问题讨论】:
标签: arrays excel vba indexing stock