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

时间:2018-03-05 02:42:00

标签: excel vba excel-vba loops integer-overflow

我有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是否不适合处理这么多数据?

1 个答案:

答案 0 :(得分: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