Having a problem with If, ElseIf, Else function in VBA.
My code needs to look for "Text1", elseIf needs to look for "Text2", else make a note in a log file.
The problem is I can't seem to change the Find parameters as part of the ElseIF..
ElseIf Selection.Find.ClearFormatting
With Selection.Find
.Forward = False
.Text = "Text2"
End With
Selection.Find.Execute Then
The ElseIF will only work if I put it infront of The execute line, this means im still searching "Text1" that doesn't exist.
ElseIf Selection.Find.Execute Then
Any idea where im going wrong?
Full code:
Sub Testing()
Dim LogFile As String
LogFile = "G:\ErrorLog.txt"
Selection.Find.ClearFormatting
With Selection.Find
.Forward = False
.Text = "Text1"
End With
If Selection.Find.Execute Then
MsgBox "Found Text1"
Selection.Find.ClearFormatting
With Selection.Find
.Forward = False
.Text = "Text2"
End With
ElseIf Selection.Find.Execute Then
MsgBox "Found Text2"
Else
Open LogFile For Append As #1
Print #1, Now & " " & "Text Field Error" & ": "
Close #1
End If
End Sub