【问题标题】:Running a loop based on input box answer根据输入框答案运行循环
【发布时间】:2013-10-05 09:55:18
【问题描述】:

如果输入框的答案是yes,我想运行一个宏。

因此,每次运行其他宏时都需要询问该问题,我需要询问该问题,直到用户在他们需要的所有日期完成宏。

我把输入框放错地方了,但不知道该放哪里。

Call ChangeDates

strMore = Application.InputBox(prompt:="Do you have any more dates to enter? Type Yes or No", Type:=2)

Do Until strMore = "no"

    If strMore = "yes" Then
        Call ChangeDates
    End If

    strMore = Application.InputBox(prompt:="Do you have any more dates to enter? Type Yes or No", Type:=2)
Loop

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    考虑将此解决方案作为使用InputBox 的替代方案:

    Sub test()  
    Question:  
    If MsgBox("Do you have any more dates to enter?", vbYesNo) = vbNo Then Exit Sub  
    Call ChangeDates  
    GoTo Question  
    
    End Sub
    

    这样您的用户(或您)就不必输入“是”或“否”来继续/结束该过程。

    【讨论】:

    • 谢谢我用了这个——因为我不知道你可以做这个问题,并认为这是一个很好的答案。 @Arich
    • @louisathompson 我很高兴它对你有用。 :) 请将我的回复标记为答案。
    【解决方案2】:

    试试这个

    Sub Sample()
        Dim strMore
    
        Do Until strMore <> ""
            strMore = Application.InputBox(prompt:="Do you have any more dates to enter? Type Yes or No", Type:=2)
    
            '~~> Check if user pressed cancel or typed "NO"
            If strMore = False Or UCase(Trim(strMore)) = "NO" Then Exit Do
    
            '~~> Check if user entered "YES"
            If UCase(Trim(strMore)) = "YES" Then
                '~~> Rest of your code
                Call ChangeDates
            End If
        Loop
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多