2016-06-10 104 views
0

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

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) 
+0

是否有可能在'lr'是永远是0?第一次运行'For'循环会发生吗?另外,在你的sub的最顶端添加Option Option,并且添加Dim a as Long(或者一个快捷方式是Dim a&')。 – BruceWayne

+0

当程序遇到错误时,“a”的值是多少?将鼠标悬停在变量“a”上以查看它是什么。我还注意到,你的lr(lastrow)变量是从B列中取出的,而不是A列中的,正如你的评论所暗示的那样。检查您感兴趣的列。 –

回答

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 
+0

您正确的错误是由于InStr的语法不正确。我没有使用太多,所以我仍然在学习如何正确使用它。代码运行,但由于某种原因它没有做任何事情。对于包含“bloomberg”的单元格,没有任何内容正在被检测到。任何想法为什么? – Brayheart

+0

哦,哇,好赶上! – BruceWayne

+0

请注意,我提供的代码是专门检查C列(这是第3列)。你想要检查哪一栏? – tigeravatar

0

试试这个:

With ActiveSheet.Cells(a, 3)