Sub stock()

Dim lastrow As LongLong
Dim lastrow_2 As LongLong
Dim current_cell As Variant
Dim next_cell As Variant
Dim ticker_row As Integer
Dim yearly_change_row As Integer
Dim open_price As Variant
Dim close_price As Double
Dim Volume As LongLong

'Create Appropriate Column Headers for Each Sheet
Dim ws As Worksheet

For Each ws In Worksheets

ws.Cells(1, 9).Value = "Ticker"
ws.Cells(1, 10).Value = "Yearly Change"
ws.Cells(1, 11).Value = "Percent Change"
ws.Cells(1, 12).Value = "Total Stock Volume"
ws.Cells(1, 16).Value = "Ticker"
ws.Cells(1, 17).Value = "Value"
ws.Cells(2, 15).Value = "Greatest % Increase"
ws.Cells(3, 15).Value = "Greatest % Decrease"
ws.Cells(4, 15).Value = "Greatest Total Volume"

'Return the Row Number of the Last Cell in the Current Worksheet

lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row


'using the variables here to start the row that the outputs will get printed to.
ticker_row = 2
yearly_change_row = 2
open_price = ws.Cells(2, 3).Value

'Iterate through the rows to check if the next ticker symbol is the same as the current symbol.

For i = 2 To lastrow

'setting the volume equal to the value in cell 2,7 to start, and adds the volume of each line until a true if is returned in order to sum a running total of the volume.

Volume = Volume + ws.Cells(i, 7).Value

'setting the current cell and next cell for each i

current_cell = ws.Cells(i, 1).Value
next_cell = ws.Cells(i + 1, 1).Value

'using the if statement to see if the ticker in current cell is the same as the next cell.

    If current_cell <> next_cell Then
        ' if it is not the same, then we will begin printing values to the summary column for the ticker name, the yearly change, percent change, and total volume.
    
        ws.Cells(ticker_row, 9).Value = current_cell
        ticker_row = ticker_row + 1
        
        close_price = ws.Cells(i, 6).Value
        
        ws.Cells(yearly_change_row, 10).Value = (close_price - open_price)
        If (ws.Cells(yearly_change_row, 10).Value) > 0 Then
            ws.Cells(yearly_change_row, 10).Interior.ColorIndex = 4
        Else
            ws.Cells(yearly_change_row, 10).Interior.ColorIndex = 3
        End If
        
        ws.Cells(yearly_change_row, 11).Value = (close_price - open_price) / open_price
        ws.Cells(yearly_change_row, 11).NumberFormat = "0.00%"
        If (ws.Cells(yearly_change_row, 11).Value) > 0 Then
            ws.Cells(yearly_change_row, 11).Interior.ColorIndex = 4
        Else
            ws.Cells(yearly_change_row, 11).Interior.ColorIndex = 3
        End If
        
        ws.Cells(yearly_change_row, 12).Value = Volume
        
        yearly_change_row = yearly_change_row + 1
        open_price = ws.Cells(i + 1, 3).Value
        Volume = 0
        
    End If
Next i

'finding the row number of the last row in the summary column of ticker symbols
lastrow_2 = ws.Cells(Rows.Count, "I").End(xlUp).Row

For j = 2 To lastrow_2
    If ws.Cells(j, 11).Value > ws.Cells(2, 17).Value Then
        ws.Cells(2, 16).Value = ws.Cells(j, 9).Value
        ws.Cells(2, 17).Value = ws.Cells(j, 11).Value
        
    End If
    
    If ws.Cells(j, 11).Value < ws.Cells(3, 17).Value Then
        ws.Cells(3, 16).Value = ws.Cells(j, 9).Value
        ws.Cells(3, 17).Value = ws.Cells(j, 11).Value
        
    End If

    If ws.Cells(j, 12).Value > ws.Cells(4, 17).Value Then
        ws.Cells(4, 16).Value = ws.Cells(j, 9).Value
        ws.Cells(4, 17).Value = ws.Cells(j, 12).Value
    End If
    
Next j

ws.Cells(3, 17).NumberFormat = "0.00%"
ws.Cells(2, 17).NumberFormat = "0.00%"

Next ws


End Sub