ElseIf语句出错

我需要从TAB2中逐行提取特定列中的值,然后在3个不同的列中search该值,并在TAB1中find更多的限制。 我需要基于指定的约束进行计数,并自动填充TAB2中相应值旁边的4列。

当我点击一个button,然后根据指定的条件执行操作,search和计数时,我得到下面的代码:

Option Compare Text Sub SO() Dim cCount As Long, ciyr1 As Long, ciyr2 As Long, csyr2 As Long Dim i As Long, inputString As String, group As String Dim yr1 As String, yr2 As String Dim inc As String, sr As String inputString = InputBox("Enter Application Name:", "Input Box Text") group = "gbl cecc sustainment" yr1 = "2013/**" yr2 = "2014/**" inc = "Incident" sr = "Request" cCount = 0 ciyr1 = 0 ciyr2 = 0 csyr1 = 0 csyr2 = 0 ThisWorkbook.Activate Set ws = Worksheets("extract") With ws For i = 2 To 15505 If InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, inc) > 0 And _ InStr(Cells(i, 14).Value, yr1) > 0 Then ciyr1 = ciyr1 + 1 'to check if value at cells(i,14) is anything that starts with "2013". (the value being present are 2013/01, 2013/02, etc..) ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ 'I get a error with a message: else without If InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, inc) > 0 And _ InStr(Cells(i, 14).Value, yr2) > 0 Then ciyr2 = ciyr2 + 1 ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, sr) > 0 And _ InStr(Cells(i, 14).Value, yr1) > 0 Then csyr1 = csyr1 + 1 ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, sr) > 0 And _ InStr(Cells(i, 14).Value, yr2) > 0 Then csyr2 = csyr2 + 1 Else: MsgBox "No matches found!!" End If Next i End With If ciyr1 < 0 Or _ ciyr2 < 0 Or _ csyr1 < 0 Or _ csyr2 < 0 Then MsgBox "Number of matches for Inc 2013" & inputString & ciyr1 MsgBox "Number of matches for Inc 2014" & inputString & ciyr2 MsgBox "Number of matches for SR 2013" & inputString & csyr1 MsgBox "Number of matches for SR 2014" & inputString & csyr2 End If End Sub 

有人可以纠正我在上面的代码中所犯的错误

也有谁可以告诉我如何从一个选项卡中取值,然后在执行循环和条件后自动填充值

在同一行上放置“if后面要做的事情”代码ciyr1 = ciyr1 + 1作为Then ,则实际上已经结束了IF块。 如果您没有其他条件,并且允许您跳过写入End If则此快捷方式非常有用。 要纠正,只需在Then之后添加一个返回值并将ciry1 = ciyr + 1移动到新行。 你将需要为你的所有行做这个。

 If InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, inc) > 0 And _ InStr(Cells(i, 14).Value, yr1) > 0 Then ciyr1 = ciyr1 + 1 'to check if value at cells(i,14) is anything that starts with "2013". (the value being present are 2013/01, 2013/02, etc..) ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ 'I get a error with a message: else without If InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, inc) > 0 And _ InStr(Cells(i, 14).Value, yr2) > 0 Then ciyr2 = ciyr2 + 1 ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, sr) > 0 And _ InStr(Cells(i, 14).Value, yr1) > 0 Then csyr1 = csyr1 + 1 ElseIf InStr(Cells(i, 1).Value, group) > 0 And _ InStr(Cells(i, 2).Value, inputString) > 0 Or _ InStr(Cells(i, 10).Value, inputString) > 0 Or _ InStr(Cells(i, 11).Value, inputString) > 0 Or _ InStr(Cells(i, 3).Value, sr) > 0 And _ InStr(Cells(i, 14).Value, yr2) > 0 Then csyr2 = csyr2 + 1 Else MsgBox "No matches found!!" End If