Excel VBA restarts the same routine internally based on VbMsgBoxResult

I am trying to reload my Sub based on MsgBoxReults. The code I have does not contain errors, but will not be restarted based on the choice of users (I hope the presence of an IF statement in another IF is not a problem)

Please, help.

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

+4
source share
3 answers

Try the code below. You need to configure the variable to get feedback from 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
source

This moves the loop to the calling 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
+1
source

From @Shai Rado answer, but without gotos or options

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
0
source

Source: https://habr.com/ru/post/1648947/


All Articles