在Select Case语句中循环没有发生错误

问题描述:

当运行此代码时,我得到一个错误,即“没有做的循环”。如果选择“案例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 

下面的代码应该做你想做的事,同时仍然保持每个“块”代码的完整性。

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语句将失败,但这是另一天的问题。