VBA宏错误

时间:2016-02-19 14:51:03

标签: excel vba excel-vba

我让这个宏在一个工作表上完美运行,但现在它在第13行显示错误。宏基本上找到以“Unique Pulls”开头的任何标题下的所有值,然后将它们全部加在一起。

我认为这与ActiveSheet有关,但我似乎无法弄明白。

       If UCase$(ActiveSheet.Cells(1, i).Value) Like "UNIQUE PULLS*" Then
            iTotal = iTotal + ActiveSheet.Cells(n, i).Value
               ' For each of these columns, take value and add to total sum
        End If

Run-time error '13': Type mismatch

2 个答案:

答案 0 :(得分:1)

当单元格的值不是数字时,您会收到此错误。您可以添加IsNumeric检查以跳过任何无数字单元格:

If UCase$(ActiveSheet.Cells(1, i).Value) Like "UNIQUE PULLS*" And IsNumeric(ActiveSheet.Cells(n, i).Value) Then
    iTotal = iTotal + ActiveSheet.Cells(n, i).Value
End If

答案 1 :(得分:0)

试试这个:

If Trim(UCase$(ActiveSheet.Cells(1, i).Text)) Like "UNIQUE PULLS*" Then
       iTotal = iTotal + ActiveSheet.Cells(n, i).Value2
       ' For each of these columns, take value and add to total sum
End If

如果你在这一行收到错误 - > iTotal = iTotal + ActiveSheet.Cells(n, i).Value2

这可能是因为ActiveSheet.Cells(n, i).Value2不是可以添加的数字,但是其他一些数据类型使用Debug.Print ActiveSheet.Cells(n, i).Value2进行检查,您将获得{I}中要添加到的总数的输出{3}}

相关问题