Do loop in userfrom

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The code below starts if the user clicks a button on the userform.

The idea is it loops until the user enters "Apples" or clicks the "No" button on the messagebox.

The problem is if the user enters the wrong value more than one, say n times, if he eventually clicks "No", the messagebox pops up n times. I want it to only pop up once.

Code:
    Dim Response As Variant

    Do Until Response = vbNo
        Select Case Me.txtbox.Value
             Case "Apples"
                 Unload Me
              Case Else
                  Unload Me
                  Response = MsgBox(Prompt:="Wrong, repeat?", Buttons:=vbYesNo)
                  If Response = vbNo Then
                      ' Do something
                  Else               
                      MyUserform.Show
                  End If
            End Select
    Loop

To fix it, I had to add an extra line, as per the below, to make it work:

Code:
Dim Response As Variant

    Do Until Response = vbNo
        Select Case Me.txtbox.Value
             Case "Apples"
                 Unload Me
              Case Else
                  Unload Me
                  Response = MsgBox(Prompt:="Wrong, repeat?", Buttons:=vbYesNo)
                  If Response = vbNo Then
                      ' Do something
                  Else               
                      MyUserform.Show

                    '**********
                    Response = vbNo 'HAD TO ADD THIS LINE TO FIX IT
                    '**********

                  End If
            End Select
    Loop

Why is that?

Thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this approach:

VBA Code:
Private Sub CommandButton1_Click()
  Dim Response As Variant
  If Me.txtbox.Value = "Apples" Then
    Unload Me
    Exit Sub
  End If
  Response = MsgBox(Prompt:="Wrong, repeat?", Buttons:=vbYesNo)
  If Response = vbNo Then
    Unload Me
    Exit Sub
  End If
  txtbox.SetFocus
End Sub
 
Upvote 0
Solution
Try this approach:

VBA Code:
Private Sub CommandButton1_Click()
  Dim Response As Variant
  If Me.txtbox.Value = "Apples" Then
    Unload Me
    Exit Sub
  End If
  Response = MsgBox(Prompt:="Wrong, repeat?", Buttons:=vbYesNo)
  If Response = vbNo Then
    Unload Me
    Exit Sub
  End If
  txtbox.SetFocus
End Sub
Thanks, your code works.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,565
Members
449,038
Latest member
Guest1337

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