声明在Type块外无效-VBA

时间:2019-01-23 12:35:38

标签: excel vba

我有下面的代码,用于合并/合并1-4列中具有相同文本的所有重复行,并对第5列中的值求和。

代码已经正常运行了一段时间,但是突然突然停止工作,并且我遇到了以下错误消息:

编译错误:

  

声明无效的外部类型块

任何帮助解决此问题的方法将不胜感激

Sub nSum()
    Dim Rng As Range, Dn As Range, n As Long, Txt As String, Ac As Long
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    ray(1 To Rng.Count, 1 To 4) 'Column count ''***ERROR LINE***************

    With CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare

        For Each Dn In Rng
            Txt = Join(Application.Transpose(Application.Transpose(Dn.Resize(, 3))), ",")
            If Not .Exists(Txt) Then
                n = n + 1
                For Ac = 1 To 4: ray(n, Ac) = Dn.Offset(, Ac - 1): Next Ac
                .Add Txt, n
            Else
                ray(.Item(Txt), 4) = ray(.Item(Txt), 4) + Dn.Offset(, 3)
            End If
        Next

        n = .Count
    End With

    With Sheets("Sheet2").Range("A1").Resize(n, 4)
        .Value = ray
        .Borders.Weight = 2
        .Columns.AutoFit
    End With
End Sub'

1 个答案:

答案 0 :(得分:2)

我猜您正在尝试声明或重新初始化名为ray的数组。

因此,单词ReDimDim被跳过了:

Sub nSum()
    Dim Rng As Range, Dn As Range, n As Long, Txt As String, Ac As Long
    Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    Dim ray(1 To Rng.Count, 1 To 4) 'Or Redim Ray(1 To ...
    'Rest of the code
End Sub