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