2013-10-05 44 views
1

如果输入框的答案是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 

回答

0

考虑这种解决方案,以替代使用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 

这样,你的用户(或你)就不用输入“是“或”否“,以便继续/结束该过程。

+0

谢谢我使用了这个 - 因为我不知道你可以做这个问题,并认为这是一个很好的答案。 @Arich –

+0

@louisathompson我很高兴它为你工作。 :)请将我的回答标记为答案。 – ARich

0

试试这个

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