2017-10-14 104 views
-2

當運行此代碼時,我得到一個錯誤,即「沒有做的循環」。如果選擇「案例vbno」,則需要返回原始輸入框。如果用戶選擇「案例vbyes」,我希望它突出顯示,然後單元格再循環返回到原始輸入框。如果選擇取消,我希望它完全退出。在Select Case語句中循環沒有發生錯誤

Sub find_highlight3() 
    Dim w As Variant 
    Dim FoundCell As Range 
    Dim ans As String 

    Do 

     w = InputBox("What to find?") 

     Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False).Activate 
     With Selection.Interior 

      Select Case MsgBox("Hellow", vbYesNoCancel) 

       Case vbNo 

    Loop 

       Case vbYes 

        .ColorIndex = 6 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 

    Loop 

       Case vbCancel 

      End Select 

     End With 
End Sub 

回答

0

下面的代碼應該做你想做的事,同時仍然保持每個「塊」代碼的完整性。

Sub find_highlight3() 
    Dim w As Variant 
    Dim FoundCell As Range 
    Dim ans As String 

    Do 
     w = InputBox("What to find?") 

     Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _ 
      LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
      MatchCase:=False).Activate 

     Select Case MsgBox("Hellow", vbYesNoCancel) 

      Case vbNo 

      Case vbYes  
       With Selection.Interior 
        .ColorIndex = 6 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
       End With 

      Case vbCancel  
       Exit Do 

     End Select 
    Loop 
End Sub 

注:如果Find不匹配任何東西(因爲Nothing.Activate無效)您Activate語句將失敗,但這是另一天的問題。