Excel VBA基于VbMsgBoxResult重新启动自身内的相同子例程

Excel VBA基于VbMsgBoxResult重新启动自身内的相同子例程,excel,if-statement,msgbox,vba,Excel,If Statement,Msgbox,Vba,我正试图让我的潜艇重新启动基于MsgBoxReults。我的代码不包含任何错误,但不会根据用户的选择重新启动(希望在另一个IF中包含IF语句不是问题) 请帮忙 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 t

我正试图让我的潜艇重新启动基于MsgBoxReults。我的代码不包含任何错误,但不会根据用户的选择重新启动(希望在另一个IF中包含IF语句不是问题)

请帮忙

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

End Sub

尝试下面的代码。您需要设置一个变量以从
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

这会将循环移动到调用子循环:

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

来自@Shai Rado的答案,但没有gotos或变体

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

@J VBA,IF中有和IF不是问题(取决于您试图实现的逻辑)。查看下面我的答案,我认为
Weather=InputBox…
需要在
Do中移动,直到
循环