VBA 中的 If、ElseIf、Else 函数存在问题。
我的代码需要查找“Text1”,elseIf 需要查找“Text2”,否则在日志文件中记录。
问题是我似乎无法将 Find 参数更改为 ElseIF 的一部分。
ElseIf Selection.Find.ClearFormatting
With Selection.Find
.Forward = False
.Text = "Text2"
End With
Selection.Find.Execute Then
ElseIF 仅在我将它放在执行行前面时才有效,这意味着我仍在搜索不存在的“Text1”。
ElseIf Selection.Find.Execute Then
知道我哪里出错了吗?
完整代码:
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