由于溢出错误,无法在整个工作簿中运行Excel宏循环

问题描述 投票:0回答:1

我有7张超过70,000个条目;我在每个工作表的单独列中按每个唯一属性求和。

虽然宏在单张纸上工作正常,但我很难在每张纸上循环我的宏,并且出现溢出错误。

我认为要么是查询的绝对数量不适合Excel,要么我需要以某种方式将输出重新定义为长整数。

Sub Summation()

Dim WS_Count As Integer
Dim I As Integer

WS_Count = ActiveWorkbook.Worksheets.Count

For X = 1 To WS_Count

    Sheets(X).Select

     Dim total As Variant

     Dim attribute As String
     Dim data_last_row As Variant
     Dim attribute_last_row As Variant

     Range("I1").Value = "Unique Attribute"
     Range("L1").Value = "Total"

     data_last_row = Cells(Rows.Count, "A").End(xlUp).Row

     For I = 2 To data_last_row
         attribute = ws.Cells(I, 1)
         total = Cells(I, 7) + total
         attribute_last_row = Cells(Rows.Count, "I").End(xlUp).Row
         If Cells(I + 1, 1) <> attribute Then
             Cells(attribute _last_row + 1, 9) = attribute 
         End If
         Cells(attribute _last_row + 1, 12) = total        
     Next I

Next X

End Sub

我也尝试了For Each ws In Worksheets并将我的单元格定义为ws,但是输出在每个后续工作表中都是关闭的,基本上复制了第一张纸上的宏的结果。

还有其他方法,或者Excel是否不适合处理这么多数据?

excel vba excel-vba loops integer-overflow
1个回答
1
投票

现在应该有点工作。

不应将attribute声明为变量,因为它是关键字

attributes _last_row中间有一个空间。可能是一个错字,但请注意。

Sub Summation()

Dim WS_Count As Integer
Dim I As Long
Dim ws As Worksheet

WS_Count = ActiveWorkbook.Worksheets.Count

For X = 1 To WS_Count

    Set ws = Sheets(X)

    Dim total As Variant


    Dim attributes As String
    Dim data_last_row As Variant
    Dim attributes_last_row As Variant

    ws.Range("I1").Value = "Unique Attribute"
    ws.Range("L1").Value = "Total"

    data_last_row = Cells(ws.Rows.Count, "A").End(xlUp).Row

    For I = 2 To data_last_row
        attributes = ws.Cells(I, 1)
        total = ws.Cells(I, 7) + total
        attributes_last_row = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row
        If Cells(I + 1, 1) <> attributes Then
             ws.Cells(attributes_last_row + 1, 9) = attributes
        End If

        ws.Cells(attributes_last_row + 1, 12) = total

    Next I

Next X

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.