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,600 points
711 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,000 points

Related Questions In Others

0 votes
0 answers

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

I want to click on a number ...READ MORE

Dec 25, 2022 in Others by Kithuzzz
• 38,000 points
1,379 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,600 points
2,730 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,600 points
2,310 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,600 points
636 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,600 points
1,096 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,600 points
1,221 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,000 points
1,856 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,000 points
1,759 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