Процедура ниже представляет собой ваш собственный код, правильно отформатированный и лишенный фатальной ошибки, вызванной помещением операторов Dim в цикл. Вы не можете затемнить переменную более одного раза в процедуре.
Будет ли код на самом деле работать или, что еще лучше, действительно делать то, что вы хотите, это другой вопрос. Вы можете попробовать. Моя цель - предоставить здесь код, который можно попробовать и исправить. Вы пытаетесь. Дайте нам знать, где и как он выходит из строя (какая строка, какая ошибка?), И вы получите помощь здесь.
Sub hw_vba()
' all of teh Dim statements following should be executed only
' once, at the beginning of the code.
' therefore they can't be inside a loop which repeats them many times.
' For Each ws In Worksheets
Dim WorksheetName As String
Dim r As Long 'r is a variable to loop through the rows
Dim volume As Long 'volume is a variable that will hold the total stock
Dim out As Long 'out is a variable to hold the output
For Each ws In Worksheets
lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row ' Determines the Last Row in ticker column
Cells(1, 9).Value = "Ticker"
Cells(1, 10).Value = "Total Volume"
out = 2
For r = 2 To lastrow 'should loop thru ticker column from first integer to the last row
If Cells(r + 1, 1).Value <> Cells(r, 1).Value Then 'will determine when the variable in column 1 changes
volume = volume + Cells(r, 7).Value 'adds the last value of the first item to volume
Cells(out, 9) = Cells(r, 1).Value 'outputs the ticker name
Cells(out, 10).Value = volume 'outputs total volume
volume = 0 'resets volume to 0 for next ticker
out = out + 1 'increases the row for the output
Else
volume = volume + Cells(r, 7).Value 'should add all those lines that are the same
End If
Next r
Next ws
End Sub