我需要从TAB2逐行提取特定列的值,然后在3个不同的列中搜索该值,并在TAB1中搜索更多的约束。我需要根据指定的约束进行计数,并在TAB2中相应值旁边自动填充4列。
我有以下代码,当我单击按钮然后根据指定的条件执行操作,搜索和计数时,我获取值。
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
有人可以纠正我在上面的代码中所犯的错误
也可以有人告诉我如何从一个标签中获取值,然后在执行循环和条件后自动填充值
答案 0 :(得分:1)
通过将ciyr1 = ciyr1 + 1
上的Then
代码作为IF
添加到End If
,您实际上已结束Then
阻止。如果您没有其他条件并且允许您跳过编写ciry1 = ciyr + 1
,则此快捷方式非常有用。要解决问题,只需在Then
之后添加退货并将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
移至新行。您需要为所有{{1}}行执行此操作。
{{1}}