【问题标题】:Loop without Do error within Select Case statement在 Select Case 语句中循环没有 Do 错误
【发布时间】:2017-10-14 00:56:04
【问题描述】:

运行此代码时,我收到一个错误,即“没有执行的循环”。我想如果选择“case vbno”然后它返回到原来的输入框。如果用户选择“case vbyes”,我希望它突出显示然后单元格然后循环返回执行并返回到原始输入框。如果选择取消,我希望它完全退出。

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

【问题讨论】:

    标签: excel do-loops select-case vba


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      相关资源
      最近更新 更多