如何解决 VBA 循环的运行时错误

如何解决 VBA 循环的运行时错误

我有一个循环遍历单元格的宏。我的范围从 开始,$A1但我需要它适用于从 开始的范围$A8。我该如何修复我的代码:

Sub test()
    Dim a, i As Long, ii As Long, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = 1
    a = Sheets("sheet1").Cells(1).CurrentRegion.Value
    For i = 2 To UBound(a, 1)
        If Not dic.exists(a(i, 1)) Then
            Set dic(a(i, 1)) = CreateObject("Scripting.Dictionary")
            dic(a(i, 1)).CompareMode = 1
        End If
        dic(a(i, 1))(a(i, 2)) = dic(a(i, 1))(a(i, 2)) + a(i, 3)
    Next
    With Sheets("sheet2").Cells(1).CurrentRegion
        .Offset(1, 1).ClearContents
        a = .Value
        For i = 2 To UBound(a, 1)
            If dic.exists(a(i, 1)) Then
                For ii = 2 To UBound(a, 2)
                 a(i, ii) = dic(a(i, 1))(a(1, ii)) + 0
             Next
            End If
        Next
        .Value = a: .Parent.Activate
    End With

子目录结束

相关内容