excel VBA -循环内的消息框

dgtucam1  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(137)

我有附加的用户表单。每当我点击“继续!”按钮时,我让它检查批次重量之和是否大于产品详细信息框中的第一个指定重量;
如果是,它要求我继续或不继续,我的答案是“是”,它继续,如果“否”,我希望它让我再次更改用户表单中的数量。
我已经写了下面的代码,但当我点击“否”的msgbox不断显示一次又一次:

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
jucafojl

jucafojl1#

尽可能避免后藤。尝试以下操作:

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

相关问题