我有一张带有交易代码的表(A),一年中的工作日(B),开盘价(C),高值(D),低值(E),收盘价(F)和音量(E)。数据按工作日时间顺序排列,并按股票代码按字母顺序排列。
我试图在第二个子命令中创建一个命令,以提取带有匹配的股票代号的第一行和最后一行。然后使用这些行号,拉出相应的年份的开始和结束值以创建一个年度更改列。我没有从代码中得到任何错误,但是每年的更改并没有提取正确的值。我想念什么?
Sub TickerVolume()
' Set an initial variable for holding the ticker
Dim Ticker As String
'Create new column headers
Cells(1, 9).Value = "Ticker"
Cells(1, 10).Value = "Yearly Change"
Cells(1, 11).Value = "Percent Change"
Cells(1, 12).Value = "Total Stock Volume"
' Set an initial variable for holding the volume total
Dim Vol_Total As Double
Vol_Total = 0
' Keep track of the location for summary table
Dim Summary_Table_Row As Integer
Summary_Table_Row = 2
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through all tickers
For i = 2 To LastRow
' Check if we are still within the same ticker, if it is not...
If Cells(i + 1, 1).Value <> Cells(i, 1).Value Then
' Set the Ticker
Ticker = Cells(i, 1).Value
' Add to the Volume Total
Vol_Total = Vol_Total + Cells(i, 7).Value
' Print the Ticker in the Summary Table
Range("I" & Summary_Table_Row).Value = Ticker
' Print the Vol to the Summary Table
Range("L" & Summary_Table_Row).Value = Vol_Total
' Add one to the summary table row for next ticker
Summary_Table_Row = Summary_Table_Row + 1
' Reset the Vol Total
Vol_Total = 0
' If the cell immediately following a row is the same Ticker...
Else
' Add to the Ticker Total
Vol_Total = Vol_Total + Cells(i, 7).Value
End If
Next i
End Sub
Sub YearlyChange()
' Find start and end rows with unique Ticker
Dim TickStartRow As Long
Dim TickEndRow As Long
Dim Summary_Table_Row As Integer
Summary_Table_Row = 2
LastRow1 = Cells(Rows.Count, 9).End(xlUp).Row
' Loop through all tickers
For i = 2 To LastRow1
'Find start and end rows
TickStartRow = Range("A:A").Find(what:=Cells(i, 9), after:=Cells(1,
1)).Row
TickEndRow = Range("A:A").Find(what:=Cells(i, 9), after:=Cells(1, 1),
SearchDirection:=xlPreivous).Row
' Print the Change in the Summary Table
Range("J" & Summary_Table_Row).Value = Range("C" & TickStartRow).Value -
Range("F" & TickEndRow).Value
' Add one to the summary table row for next ticker
Summary_Table_Row = Summary_Table_Row + 1
Next i
End Sub
答案 0 :(得分:2)
您的问题可能是在
TickEndRow = Range("A:A").Find(what:=Cells(i, 9), after:=Cells(1, 1), SearchDirection:=xlPreivous).Row
您拼写错误的xlPrevious
并且没有Option Explicit
的情况下,编译器只是假设您所称的xlPreivous
为空变量。将Option Explicit
添加到模块代码的顶部将有助于捕获此类小的拼写错误。尝试纠正它,看看会得到什么。