I have a table with Ticker Symbols (A), workdays over the course of a year (B), opening value (C), high value (D), low value (E), closing value (F), and volume (E). The data is chronological by workday and alphabetical by Ticker Symbol.
I am trying to create in the second sub a command that pulls the first and last row numbers with matching ticker symbols. Then using those row numbers, pull the corresponding opening and closing values for the year to create a yearly change column. I don’t get any errors from my code, but the yearly change is not pulling the correct value. What am I missing?
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
Your problem is probably that in
TickEndRow = Range("A:A").Find(what:=Cells(i, 9), after:=Cells(1, 1), SearchDirection:=xlPreivous).Row
you’ve spelled xlPrevious
incorrectly and without Option Explicit
the compiler is just assuming an empty variable for what you’ve called xlPreivous
. Adding Option Explicit
to the top of your module code will help catch small typo errors like this. Try correcting that and see what you get.
Tags: excelexcel, vba