2016-07-24 63 views
4

我試圖讓我的Sub基於MsgBoxReults重新啓動。我的代碼不包含任何錯誤,但不會根據用戶的選擇重新啓動(希望在另一個IF中有IF語句不是問題)Excel VBA基於VbMsgBoxResult重新啓動相同的子程序本身

請協助。

Sub ContinueWeatherList() 

Dim Weather As String 
'Assigning a Message Box result as a Variable for Yes/No 
Dim MoreWeather As VbMsgBoxResult 

Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
    Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
    Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
    MsgBox "Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo 

    'Using IF statement to decide what happens for each condition 
    If MoreWeather = vbYes Then 
     ''Call' command won't reinitiate Sub/*NEED TO FIX* 
     Call ContinueWeatherList 
    Else 
     MsgBox "Thank you for you input.", vbInformation 
    End If 

End If 

結束子

+0

@J VBA,擁有和中頻IF ISN內這個問題(取決於你試圖達到的邏輯)。看到我的回答如下 –

回答

1

嘗試下面的代碼。您需要設置變量以獲取VBYesNo MsgBox的反饋。

Option Explicit 

Sub ContinueWeatherList() 

Dim Weather As String 
'Assigning a Message Box result as a Variable for Yes/No  
Dim MoreWeather As Variant 

' add label to restart to 
ContinueWeatherList_Restart: 
Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
    Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
    Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
    MoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 

    'Using IF statement to decide what happens for each condition 
    If MoreWeather = vbYes Then 
     ' use GOTo command and label to reinitiate the sub 
     GoTo ContinueWeatherList_Restart 
    Else 
     MsgBox "Thank you for you input.", vbInformation 
    End If 

End If 

End Sub 
1

這使循環給調用子:

Sub EnterWeatherListItems() 
Dim MoreWeather As VbMsgBoxResult 

MoreWeather = vbYes 
Do While MoreWeather = vbYes 
    Call FillWeatherList 
    'Assigning a Message Box result as a Variable for Yes/No 
    'Using IF statement to decide what happens for each condition 
    MoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 
Loop 
MsgBox "Thank you for you input.", vbInformation 
End Sub 

Sub FillWeatherList() 
Dim Weather As String 
Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

If Weather = "" Then 
    MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
Else 
    ActiveSheet.Range("C1").End(xlDown).Offset(1, 0).Value = ActiveSheet.Range("C1").End(xlDown) + 1 
    ActiveSheet.Range("A1").End(xlDown).Offset(1, 0).Value = ActiveSheet.Range("A1").End(xlDown) + 1 
    ActiveSheet.Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
    Columns("A:C").EntireColumn.AutoFit 
End If 
End Sub 
0

從@Shai瑞士雷達表的答案,但沒有goto方法或變

Option Explicit 

Sub ContinueWeatherList() 

    Dim Weather As String 
    'Assigning a Message Box result as a Variable for Yes/No 
    Dim NoMoreWeather As Boolean 

    ' Loop until user says otherwise 
    Do Until NoMoreWeather = vbNo 
     Weather = InputBox("Type in the weather for " & Range("C1").End(xlDown) + 1) 

     If Weather = "" Then 
      MsgBox ("No data entered. Your response has not been recorded"), vbExclamation 
     Else 
      Range("C1").End(xlDown).Offset(1, 0).Value = Range("C1").End(xlDown) + 1 
      Range("A1").End(xlDown).Offset(1, 0).Value = Range("A1").End(xlDown) + 1 
      Range("B1").End(xlDown).Offset(1, 0).Value = Weather 
      Columns("A:C").EntireColumn.AutoFit 
      NoMoreWeather = MsgBox("Thank you for entering your data " & vbNewLine & "Would you like to enter another?", vbYesNo) 

     End If 

    Loop 

End Sub 
+2

我認爲'Weather = InputBox ...'需要在'Do Until'循環中移動 – YowE3K

+0

你是對的! – Shodan