VBA - Msgbox inside a Loop

0 votes

I've got the userform attached. Every time I click the "Proceed!" button, I have it verify that the weight of all batches combined is greater than the weight that was initially assigned in the Product Details box;

If I say yes, it prompts me to move on or not; if I say no, I want the userform to allow me to adjust the quantities once more.

I've typed the code below, however whenever I select "No," the message box repeatedly appears:

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
Mar 19, 2023 in Others by narikkadan
• 63,420 points
451 views

1 answer to this question.

0 votes

 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
answered Mar 19, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
1 answer

Excel VBA: Open Hyperlinks in a loop and copy paste download link in a sheet

Refer this tutorial for your solution: https://evermap.com/Tutorial_AB ...READ MORE

answered Dec 25, 2022 in Others by narikkadan
• 63,420 points
913 views
0 votes
1 answer

How to increment the Range of a For Each loop - Excel VBA

Your formula seems to sum 1 single ...READ MORE

answered Jan 7, 2023 in Others by narikkadan
• 63,420 points
1,965 views
0 votes
1 answer

VBA Loop to select then copy a range of cells based on value in column B

Try this: Sub Macro2() Dim ...READ MORE

answered Mar 23, 2023 in Others by narikkadan
• 63,420 points
1,536 views
0 votes
1 answer

Excel VBA userform paste text in expanded format (not just to one cell)

It should work if you simply supply ...READ MORE

answered Jan 21, 2023 in Others by narikkadan
• 63,420 points
353 views
0 votes
1 answer

Excel VBA Scheduled message pop up box reminder

What exactly does W11 contain? In Excel, ...READ MORE

answered Feb 3, 2023 in Others by narikkadan
• 63,420 points
618 views
0 votes
1 answer

EXCEL Userform - Creating multiple Labels and Textboxes with specific names

Please, test the next scenario: Insert a class ...READ MORE

answered Feb 9, 2023 in Others by narikkadan
• 63,420 points
755 views
0 votes
1 answer
0 votes
1 answer

VBA - Dynamic Variable Name in a Loop

Set the array values with: Dim i as ...READ MORE

answered Mar 19, 2023 in Others by Kithuzzz
• 38,010 points
1,107 views
0 votes
1 answer

Excel VBA- How to loop through specific sheets in a workbook and format the same ranges in each sheet

Range(...) instructs VBA to always use the ...READ MORE

answered Mar 21, 2023 in Others by Kithuzzz
• 38,010 points
1,036 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP