Nested If Functions with yes no message boxes

HelzBelz

New Member
Joined
Oct 16, 2014
Messages
2
Hi

I am trying to use VBA to produce a series of message boxes depending on the response to each one.

The whole series is based on a value in A1 and a comment that the user has put in B1


This is a description of the type of thing I want to do:

If value in cell A1 = 0 Then
Bring up message box 1 which includes the text value of B1 and ask the question “has the user included a comment in B1?”

If the answer is yes then bring up message box 2 asking “does the comment indicate that 0 is correct?”
If message box 2 is yes then close message box 2 and go back to cell A1
If message box 2 is no then bring up message box 3 asking “does the comment indicate data is missing?”
If message box 3 is Yes include the text “please include the data” in cell C1
If message box 3 is no include the text “zero cases is correct” in cell C1
If message box 1 is no then include the text “please include a comment in B1” in cell C1

The problem I’m getting is that there are a whole load of nested conditions based on the first message box being ‘Yes’ before I can specify what should happen if the first message box is no.

I can’t get the code to work and some elements don’t seem to run (e.g. where a response to one message box should bring up another it doesn’t always seem to do so and just does nothing instead).

Any suggestions on what the right format would be for writing this code please?

Many thanks

Helen
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub messageBoxInception()

    If Cells(1, 1) <> 0 Then Exit Sub
    
    answer1 = MsgBox("The comment placed is: " & Cells(1, 2) & vbNewLine & _
            "Has the user included a comment in B1?", vbYesNo)
            
        Select Case answer1
            Case 6 'YES
                answer2 = MsgBox("Does the comment indicate that a value of 0 is correct?", vbYesNo)
                    Select Case answer2 '
                        Case 6 'YES
                            Cells(1, 1).Activate
                            Exit Sub
                        Case 7 'NO
                            answer3 = MsgBox("Does the comment indicate that data is missing?", vbYesNo)
                            Select Case answer3
                                Case 6
                                    Cells(1, 3) = "Please include the data"
                                    Exit Sub
                                Case 7
                                    Cells(1, 3) = "Zero cases is correct"
                                    Exit Sub
                            End Select
                    End Select
                
            Case 7 'NO
                Cells(1, 3) = "Please include a comment in B1"
            Case Else
        End Select
End Sub

Something like this. I'd continue but...lunch time. It might actually be done....I don't know.
 
Upvote 0
Code:
Sub YesOrNo()
If Range("A1").Value = 0 Then
    If MsgBox(Range("B1").Value & vbNewLine & "Has the user included a comment in B1?", vbYesNo) = vbYes Then
        If MsgBox("Does the comment indicate that 0 is correct?", vbYesNo) = vbYes Then
            Range("A1").Activate
        Else
            If MsgBox("Does the comment indicate data is missing?", vbYesNo) = vbYes Then
                Range("C1").Value = "please include the data"
            Else
                Range("C1").Value = "zero cases is correct"
            End If
        End If
    Else
        Range("C1").Value = "please include a comment in B1"
    End If
End If
End Sub

hope that helps
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top