已经有“做”,但仍然有错误“没有做循环”,Excel的VBA

时间:2018-09-07 09:59:25

标签: excel vba excel-vba loops

我正在尝试创建一个用户表单来为每个销售订单输入数据,一个订单可以包含许多产品,所以我写如下。我添加了“ do ... loop”以在SO中添加其他产品,但出现了“ loop not do”错误。 如果您可以帮助我找出此错误的解决方法,或者可以向我展示执行此任务的更好方法,则非常感谢。 谢谢。

Private Sub button_OK_Click()
Dim nbr As Integer
Dim SOno As Integer
Dim choice As Integer
SOno = Sheets("engine").Range("B2")
Do
    nbr = WorksheetFunction.CountA(Sheets("SO").Range("c4:C50")) + 1 'no. of rows
    Sheets("SO").Range("r_cell").Offset(nbr, 0) = nbr
    Sheets("SO").Range("r_cell").Offset(nbr, 1) = "SO" & WorksheetFunction.Text(SOno, "00000")
    Sheets("SO").Range("r_cell").Offset(nbr, 2) = cus_name
    Sheets("so").Range("r_cell").Offset(nbr, 3) = prd_name
    Sheets("SO").Range("r_cell").Offset(nbr, 6) = qty
choice = MsgBox("Do you want to add new product for current SO?", vbYesNo)
If choice = vbYes Then
Loop
Else
Unload input_SO_UF
End If
Sheets("engine").Range("B2") = SOno + 1 'max SO no
End Sub

1 个答案:

答案 0 :(得分:2)

尝试:

Private Sub button_OK_Click()
    Dim nbr As Integer
    Dim SOno As Integer
    SOno = Sheets("engine").Range("B2")
    Do
        nbr = WorksheetFunction.CountA(Sheets("SO").Range("c4:C50")) + 1 'no. of rows
        Sheets("SO").Range("r_cell").Offset(nbr, 0) = nbr
        Sheets("SO").Range("r_cell").Offset(nbr, 1) = "SO" & WorksheetFunction.Text(SOno, "00000")
        Sheets("SO").Range("r_cell").Offset(nbr, 2) = cus_name
        Sheets("so").Range("r_cell").Offset(nbr, 3) = prd_name
        Sheets("SO").Range("r_cell").Offset(nbr, 6) = qty
    Loop While vbYes = MsgBox("Do you want to add new product for current SO?", vbYesNo)

    Unload input_SO_UF
    Sheets("engine").Range("B2") = SOno + 1 'max SO no
End Sub