1

I have the attached userform. Whenever I click on the "Proceed!" button, I have it check if the sum of batches weight is more than the first assigned weight in the Product Details frame;

if yes it asks me to proceed or not, I the answer was "Yes" it continues, If "No" I want it to let me change the quantities again in the userform.

I have written the below code but when I hit "No" the msgbox keeps showing again and again:

Private Sub CommandButton1_Click()     'Proceed! Button
Dim answer As Integer

q = Val(Left(Label2.Caption, 5))       'Weight in Product Details --> 15.12 tons 

Total = BatchTotal1 + BatchTotal2 + BatchTotal3 + BatchTotal4 + BatchTotal5  'Publicly dimmed previously

Again:

If Total > q Then
    answer = MsgBox("Batches total weight is more than you assigned first, Do you want to proceed?", vbQuestion + vbYesNo)
    If answer = vbYes Then
        GoTo Continue
    Else
        GoTo Again
    End If
End If

Continue:

'Another code
1
  • How do you recalculate q or Total if you never return to this in your loop. Possibly a Do - While loop might be better than Goto? Commented Mar 18, 2023 at 9:38

1 Answer 1

0

Avoid GoTo if at all possible. Try this:

Private Sub CommandButton1_Click()     'Proceed! Button

    Dim answer As Long

    q = Val(Left(Label2.Caption, 5))       'Weight in Product Details --> 15.12 tons 

    Total = BatchTotal1 + BatchTotal2 + BatchTotal3 + BatchTotal4 + BatchTotal5  'Publicly dimmed previously
    If Total > q Then
        answer = MsgBox("Batches total weight is more than you assigned first, Do you want to proceed?", vbQuestion + vbYesNo)
        If answer <> vbYes Then
            Exit Sub
        Else
    End If

    ' Another code

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a particular reason why you're not using If answer = vbNo ...? BTW, the Else needs to be End If.
@VBasic2008: Well, it required less editing ... but it will also let the code work as intended in case vbYesNoCancel should be used for some reason.
@Gustav I will try it, but in this case what will the No button do?
Yes, it will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.