2013-04-30 93 views
2

If,ElseIf,Else在VBA中的功能有問題。VBA IF ElseIF Else

我的代碼需要查找「Text1」,否則如果需要查找「Text2」,請在日誌文件中記錄一下。

的問題是,我似乎無法改變查找參數作爲ELSEIF的一部分。這

ElseIf Selection.Find.ClearFormatting  
With Selection.Find 
    .Forward = False 
    .Text = "Text2" 
End With            
Selection.Find.Execute Then 

,如果我把它放在盈方執行線的ELSEIF只會工作,這仍然意味着即時通訊搜索不存在的「文本1」。

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 

回答

4
**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** 

看的** ......這些都在尋找相同。它如何通過相同的方式彈出一個值?至少它只能找到文本1或其他,但不能ElseIf。 所以你應該刪除ElseIf只能成爲If

+0

對了!謝謝! – 2013-05-01 01:21:12