4

I'm trying to get my Sub to restart based on MsgBoxReults. The code I have doesn't contain any errors, but won't restart based on the users choice (hopefully, having an IF statement within another IF isn't the issue)

Please assist.

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

Community
  • 1
  • 1
J VBA
  • 178
  • 2
  • 5
  • @J VBA, having and IF inside the IF isn't the issue (depends on the logic you are trying to achieve). See my answer below – Shai Rado Jul 24 '16 at 17:06

3 Answers3

2

Try the code below. You need to setup a variable to get the feedback from the 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
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
1

This moves the loop to a 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
Doug Glancy
  • 27,214
  • 6
  • 67
  • 115
0

From @Shai Rado's answer but without gotos or variants

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
YowE3K
  • 23,852
  • 7
  • 26
  • 40
Shodan
  • 1,065
  • 2
  • 13
  • 35