在VBA中执行Like Operator时运行时错误

我试图devise一个函数,在第一行search一个特定的术语,然后在术语“Requirement”或“Function Change”中search该列。 一旦find这些术语,就是search包含这些术语的行,并在不同的列中检查术语“协议”。 我试图用Like操作符来完成这个任务,但是我一直在popup一个“应用程序定义或对象定义的错误”。 有人可以弄清楚为什么我可能会得到这个错误? 我已经看了一会儿,不知道该怎么办。 这是我迄今为止的代码:

编辑:代码到达第一个IF语句时popup错误

Function CountProtocol() As Long Sheets("CS-CRM Raw Data").Select Sheets("CS-CRM Raw Data").Unprotect LastRow = Range("A" & Rows.Count).End(xlUp).row LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column For i = 1 To LastRow If Cells(i, myTypeCol).Value Like "Functional Change" Or "Requirement" Then If Cells(i, myDescCol).Value Like "*protocol*" Then pro_count = pro_count + 1 End If End If Next i MsgBox "Requests of type ""Requirement"" or ""Functional Change"" that have ""Protocol"" in the description: " & pro_count CountProtocol = Pro End Function 

编辑:这里是myTypeCol分配的代码:

 Function ColSearch(Heading As String) As Integer Sheets("CS-CRM Raw Data").Select Sheets("CS-CRM Raw Data").Unprotect myCol = Sheets("CS-CRM Raw Data").Cells.Find(What:=Heading, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Column ColSearch = myCol End Function 

这是在这样的主子程序中调用:

 myTypeCol = ColSearch("type") myDescCol = ColSearch("description") 

编辑:这是另一个函数,我调用myTypeCol,没有错误,工作正常。

 Function CountType() As Long Sheets("CS-CRM Raw Data").Select Sheets("CS-CRM Raw Data").Unprotect Dim type_count As Long Dim type_count2 As Long Dim type_sum As Long LastRow = Range("A" & Rows.Count).End(xlUp).row LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column type_count = Application.WorksheetFunction.CountIf(Range(myTypeCol & "2:" & myTypeCol & LastRow), "Requirement") type_count2 = Application.WorksheetFunction.CountIf(Range(myTypeCol & "2:" & myTypeCol & LastRow), "Functional Change") type_sum = type_count + type_count2 MsgBox "Requests of type ""Requirement"" or ""Functional Change"": " & type_sum CountType = Count End Function 

您需要比较Or操作符之后的单元格值,如下所示:

 Function CountProtocol() As Long Sheets("CS-CRM Raw Data").Select Sheets("CS-CRM Raw Data").Unprotect LastRow = Range("A" & Rows.Count).End(xlUp).row LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column For i = 1 To LastRow If Cells(i, myTypeCol).Value Like "Functional Change" Or Cells(i, myTypeCol).Value Like "Requirement" Then If Cells(i, myDescCol).Value Like "*protocol*" Then pro_count = pro_count + 1 End If End If Next i MsgBox "Requests of type ""Requirement"" or ""Functional Change"" that have ""Protocol"" in the description: " & pro_count CountProtocol = Pro End Function