2017-10-14 14 views
-2

이 코드를 실행할 때 "루프하지 않음"오류가 발생합니다. "case vbno"가 선택되면 원래 입력 상자로 돌아갑니다. 사용자가 "case 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 

참고 : (Nothing.Activate이 유효하지 않기 때문에)에 Find 아무것도 일치하지 않는 경우 귀하의 Activate 문이 실패합니다,하지만 그건 다른 날에 대한 질문입니다.