Ошибка переполнения в VBA, которая не может быть исправлена ​​путем изменения переменной на long - PullRequest
0 голосов
/ 21 января 2019

Я новичок в кодировании.скрипт будет правильно выводить мою первую переменную, а затем выдаст ошибку переполнения.

   Sub hw_vba()

   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 
   volume
   Dim out As Long     'out is a variable to hold the output

    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

1 Ответ

0 голосов
/ 21 января 2019

Процедура ниже представляет собой ваш собственный код, правильно отформатированный и лишенный фатальной ошибки, вызванной помещением операторов 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...