我正在尝试设计一个搜索第一行某个术语的函数,然后在该列中搜索“需求”或“函数更改”这两个术语。一旦找到这些术语,就要搜索包含这些术语的行,并在不同的列中检查术语“协议”。我正在尝试使用Like运算符完成此任务,但我不断弹出“应用程序定义或对象定义的错误”。任何人都可以找出我可能会收到此错误的原因吗?我已经看了一段时间了,无法弄明白。这是我到目前为止的代码:
编辑:当代码到达第一个IF语句时弹出错误
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
答案 0 :(得分:2)
您需要在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