Nested Yes/No questions to get to correct userform

cjlittlet

New Member
Joined
Dec 2, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that has multiple user forms attached to it. I'm trying to ask a series of questions to get the user to the right form. Below are the 2 scripts I've been trying to get to work. I don't care which one ends up working, I just need one of them to work right :p HELP????

code problems:
vbYesNoDemo works perfect until it gets to the question "do they need to file a claim" then no matter what I choose it just keeps going in order instead of going to the answer chosen
vbrecReturned_Cards_Click is the same except if you only answer yes to the follow up questions you get to the right place, you run into a problem if you try and choose no

VBA Code:
Sub vbYesNoDemo()
Dim userResponse As Integer
userResponse = MsgBox("Is the name on the card different than the name of the person handing you the card?", vbYesNo)

If userResponse = vbYes Then
    Call frmReturned_Cards.Show
    Exit Sub
Else: userResponse = vbNo
    Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If vbYes Then
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            If vbYes Then
            Call frmNeed_To_File_Wks_Pd.Show
            ElseIf vbNo Then Call recNot_Paid_Need_Click
        ElseIf vbNo Then
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            If vbYes Then
            Call frmNo_Need_Wks_Pd.Show
            Else: Call frmNo_Need_No_Wks_Pd.Show

End If
End If
End If
End If
End Sub

VBA Code:
Sub recReturned_Cards_Click()
Dim Answer As Integer

Answer = MsgBox("Is the name on the card different than the name of the person handing you the card?", vbQuestion + vbYesNo, "Who's card is it?")
Select Case Answer
    Case vbYes
    frmReturned_Cards.Show
    Case vbNo
    Answer = MsgBox("Do they need to file a claim?", vbQuestion + vbYesNo, "Need to file")
        Select Case Answer
        Case vbYes
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbQuestion + vbYesNo, "Weeks Paid")
            Select Case Answer
            Case vbYes
            frmNeed_To_File_Wks_Pd.Show
            Case vbNo
            frmNot_Paid_Need_Click.Show
        Case vbNo
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbQuestion + vbYesNo, "Weeks Paid")
            Select Case Answer
            Case vbYes
            frmNo_Need_To_File_Wks_Pd.Show
            Case vbNo
            recNot_Paid_Not_Need_Click
           
End Select
End Select
End Select
End Select


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi,

welcome to forum

Glancing at your code where you identify the problem

Rich (BB code):
Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If vbYes Then

Your Variable Answer is not being used to test the result as shown in BOLD which may explain your issue

try updating your code as shown below & see if helps

Rich (BB code):
Dim Answer As VbMsgBoxResult

Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If Answer = vbYes Then

Also, sensible to declare the variable with MsgBox enumeration.

Dave
 
Upvote 0
In the first one:
VBA Code:
        If vbYes Then
Needs to be
VBA Code:
        If Answer = vbYes Then
in three places.

I don't see an obvious problem in the second one. Harder to diagnose if I can't run the code.
 
Upvote 0
Hi,

welcome to forum

Glancing at your code where you identify the problem

Rich (BB code):
Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If vbYes Then

Your Variable Answer is not being used to test the result as shown in BOLD which may explain your issue

try updating your code as shown below & see if helps

Rich (BB code):
Dim Answer As VbMsgBoxResult

Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If Answer = vbYes Then

Also, sensible to declare the variable with MsgBox enumeration.

Dave
Wooo Hooo Progress!!!! No
 
Upvote 0
Hi,

welcome to forum

Glancing at your code where you identify the problem

Rich (BB code):
Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If vbYes Then

Your Variable Answer is not being used to test the result as shown in BOLD which may explain your issue

try updating your code as shown below & see if helps

Rich (BB code):
Dim Answer As VbMsgBoxResult

Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)     'issue starts here
        If Answer = vbYes Then

Also, sensible to declare the variable with MsgBox enumeration.

Dave
Sorry my last post wasn't done..... Woo Hoo Progress!!!! Now I can get a little further.... Any Idea why I can't say No to do they need to file a claim? If I say yes then everything works like a dream? I'm not sure what you mean by declaring with enumeration? you mean numbering the message boxes? can you give me an example?

VBA Code:
Sub vbYesNoDemo()
Dim userResponse As Integer
userResponse = MsgBox("Is the name on the card different than the name of the person handing you the card?", vbYesNo)

If userResponse = vbYes Then
    Call frmReturned_Cards.Show
    Exit Sub
Else: userResponse = vbNo
    Answer = MsgBox("Do they need to file a claim?", vbYesNo + vbQuestion)               'As long as I don't answer No here the next set of questions works
        If Answer = vbYes Then
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            If Answer = vbYes Then
            Call frmNeed_To_File_Wks_Pd.Show
            ElseIf Answer = vbNo Then Call recNot_Paid_Need_Click
        ElseIf Answer = vbNo Then
        Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            If Answer = vbYes Then
            Call frmNo_Need_Wks_Pd.Show
            Else: Call frmNo_Need_No_Wks_Pd.Show

End If
End If
End If
End If
End Sub
 
Upvote 0
I'm not sure what you mean by declaring with enumeration? you mean numbering the message boxes? can you give me an example?
You declared Answer and UserResponse as Integer. This should work and should not be causing your problem. However, it is usually a safer when to use an Enum data type when it's available. MsgBox returns a specific set of possible values, and if you declare Answer as vbMsgBoxResult (as shown in dmt32's post)
it can contain only those values. [MS-VBAL]: VbMsgBoxResult
 
Upvote 0
Hi,
as already explained, MsgBox returns an integer but has it's own Enumeration which if you declare your variable with (as I have shown) it will display the IntelliSense (list of available values).

See if this update to your code does what you want

VBA Code:
Sub vbYesNoDemo()
    Dim userResponse As VbMsgBoxResult, Answer As VbMsgBoxResult
    
    userResponse = MsgBox("Is the name On the card different than the name of the person handing you the card?", vbYesNo)
    
    If userResponse = vbYes Then
    
        Call frmReturned_Cards.Show
        
    Else 'No
    
        Answer = MsgBox("Do they need To file a claim?", vbYesNo + vbQuestion)
        
        If Answer = vbYes Then
        
            Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            
            If Answer = vbYes Then
                Call frmNeed_To_File_Wks_Pd.Show
                
            Else 'No
                Call recNot_Paid_Need_Click
            End If
            
        Else 'No
        
            Answer = MsgBox("Have any weeks paid out under the fictitious claim?", vbYesNo + vbQuestion)
            
            If Answer = vbYes Then
                Call frmNo_Need_Wks_Pd.Show
                
            Else 'No
                Call frmNo_Need_No_Wks_Pd.Show
            End If
            
        End If
        
    End If
        
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,430
Messages
6,119,447
Members
448,898
Latest member
drewmorgan128

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