类型不匹配错误VBA - 为什么?

时间:2016-06-10 17:06:56

标签: excel vba excel-vba

我有一个基本的宏,它正在查看B列中的Cell,然后根据我正在寻找的标准将“NA”放在C列的单元格旁边。我有类型不匹配错误,我不明白为什么。

Sub badURLs()
    Dim lr As Long ' Declare the variable
    lr = Range("B2:B23068").End(xlUp).Row ' Set the variable
    ' lr now contains the last used row in column A

    Application.ScreenUpdating = False

    For a = lr To 1 Step -1
        If InStr(1, a, "bloomberg" Or "wiki" Or "hoovers", vbTextCompare) > 0 Then
        'Compares for bloomberg, wiki, or hoovers. Enters loop if value is greater than 0
            With Cells(a, 3)
                .NumberFormat = "General"
                .Value = "NA"
            End With
        End If
    Next a

    Application.ScreenUpdating = True
End Sub

此处出现不匹配错误:

With Cells(a, 3)

2 个答案:

答案 0 :(得分:4)

您确定在With Cells(a, 3)行上收到错误吗?我想你会在If InStr行上得到错误,因为该行是完全无效的语法。它应该是:

    If InStr(1, Cells(a, 3), "bloomberg", vbTextCompare) > 0 _
    Or InStr(1, Cells(a, 3), "wiki", vbTextCompare) > 0 _
    Or InStr(1, Cells(a, 3), "hoovers", vbTextCompare) > 0 Then

答案 1 :(得分:0)

试试这个:

    With ActiveSheet.Cells(a, 3)