Burrgogi
Well-known Member
- Joined
- Nov 3, 2005
- Messages
- 514
- Office Version
- 2010
- Platform
- Windows
The following is a straight forward macro that I wrote. It has a string variable named "Key_Word" and the macro is designed to search for that key_word in 3 different worksheets. The final step is to check if the macro contains the letter Y. If it does not, then I want to display a message box "Today's answer does contain any Y", and then quit. You can jump down to the end where I've got the following comment line:
It mostly works but it keeps tripping up on the very last section of the macro. It does display the message box when the word doesn't contain any Y, but it errors after that and doesn't know what to do. I then inserted a new line: "On Error GoTo 0" to take care of the VBE error, but for some reason I can't figure out, it is still tripping up after the message box.
Just learning some VBA and so my error trapping skills are still newbish. Some help would be appreciated.
To be clear, I am getting a "Code Execution has been interrupted". When I click on 'Debug', the End If line is highlighted. I've tried swapping the order of the "Exit Sub" line and the "End If" line around but it doesn't seem to make any difference.
VBA Code:
' Condition 3: If the word does NOT contain "Y" anywhere
It mostly works but it keeps tripping up on the very last section of the macro. It does display the message box when the word doesn't contain any Y, but it errors after that and doesn't know what to do. I then inserted a new line: "On Error GoTo 0" to take care of the VBE error, but for some reason I can't figure out, it is still tripping up after the message box.
Just learning some VBA and so my error trapping skills are still newbish. Some help would be appreciated.
VBA Code:
Sub Find_Key_Word ()
With UONS
On Error Resume Next
lastRow3 = UONS.Range("A" & Rows.Count).End(xlUp).Row
Set c = UONS.Range("A2:A" & lastRow3).Find(Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not c Is Nothing Then
c.Value = c.Value & MarkIt
Else
MsgBox "Could not find: " & Key_Word & " in the" & UONS & "worksheet!"
End If
End With
With PARS
On Error Resume Next
Set c2 = PARS.Range("A2:A" & lastRow3).Find(Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not c2 Is Nothing Then
c2.Value = c2.Value & MarkIt
c2.Offset(, 1).Value = 1
Else
MsgBox "Could not find: " & Key_Word & " in the" & PARS & "worksheet!"
End If
End With
With CSET
On Error Resume Next
Set c3 = CSET.Range("A2:A" & lastRow3).Find(Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not c3 Is Nothing Then
c3.Offset(, 1).Value = 1
Else
MsgBox "Could not find: " & Key_Word & " in the" & CSET & "worksheet!"
End If
End With
' this section processes words that end in Y or contain Y
' Condition 1: Check if the word ends with "y"
If Key_Word Like "*y" Then
' Search for the word in "Ends with Y" sheet
Set foundCell = wsEndsWithY.Cells.Find(What:=Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
Set foundCell2 = wsContainsY.Cells.Find(What:=Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not foundCell Is Nothing Then
'foundCell.Font.Bold = True
foundCell.Value = foundCell.Value & MarkIt
End If
If Not foundCell2 Is Nothing Then
'foundCell2.Font.Bold = True
foundCell2.Value = foundCell2.Value & MarkIt
foundCell2.Offset(, 1).Value = 1
End If
End If
' Condition 2: Check if the word contains "Y" anywhere else
If InStr(1, Key_Word, "Y", vbTextCompare) > 0 And Not (keyword Like "*y") Then
' Search for the word in "Contains letter Y" sheet
Set foundCell = wsContainsY.Cells.Find(What:=Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
Set foundCell2 = wsContainsY2.Cells.Find(What:=Key_Word, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not foundCell Is Nothing Then
'foundCell.Font.Bold = True
foundCell.Value = foundCell.Value & MarkIt
foundCell.Offset(, 1).Value = 1
End If
If Not foundCell2 Is Nothing Then
'foundCell2.Font.Bold = True
foundCell2.Value = foundCell2.Value & MarkIt
foundCell2.Offset(, 1).Value = 1
End If
End If
' Condition 3: If the word does NOT contain "Y" anywhere
On Error GoTo 0
If InStr(1, Key_Word, "Y", vbTextCompare) = 0 Then
MsgBox "Today's answer does not contain any Y!"
Exit Sub
End If
End Sub
To be clear, I am getting a "Code Execution has been interrupted". When I click on 'Debug', the End If line is highlighted. I've tried swapping the order of the "Exit Sub" line and the "End If" line around but it doesn't seem to make any difference.
VBA Code:
MsgBox "Today's answer does not contain any Y!"
End If
Exit Sub
Last edited: