Excel 如果满足特定条件,如何跳过do循环迭代
如果我的问题含糊不清,我道歉。我有一个代码,它根据参加聚会的人数计算公共汽车旅行的费用。守则首先必须决定当事人是20岁以下还是120岁以上。如果符合该标准,我想返回一个消息框,说明该方是要容纳小型还是大型,然后在我的电子表格中将价格和输入设置为0。退出do将不起作用,因为它将完全结束循环。另外,我不想退出sub,因为我需要检查多行数据并计算成本。如果我没有设置任何类型的退出,它会将这些值设置为0,但会继续计算过程并最终留下错误的值,因此我需要它跳到循环的末尾,然后再次运行循环 谢谢Excel 如果满足特定条件,如何跳过do循环迭代,excel,vba,Excel,Vba,如果我的问题含糊不清,我道歉。我有一个代码,它根据参加聚会的人数计算公共汽车旅行的费用。守则首先必须决定当事人是20岁以下还是120岁以上。如果符合该标准,我想返回一个消息框,说明该方是要容纳小型还是大型,然后在我的电子表格中将价格和输入设置为0。退出do将不起作用,因为它将完全结束循环。另外,我不想退出sub,因为我需要检查多行数据并计算成本。如果我没有设置任何类型的退出,它会将这些值设置为0,但会继续计算过程并最终留下错误的值,因此我需要它跳到循环的末尾,然后再次运行循环 谢谢 Dim N
Dim N As String 'N = Name
Dim D As Date 'D = Date
Dim P As Integer 'P = Number of people
Dim H As Single 'H = Number of hours
Dim NS As Integer 'NS = Number of Small Buses
Dim NL As Integer 'NL = Number of Large Buses
Dim BP As Currency 'BP = Base Price
Dim OH As Single 'OH = Overtime Hours
Dim OC As Currency 'OC = Overtime Charge
Dim TP As Currency 'TP = Total Price
Dim PPBR As Currency 'PPBR = Per-person Base Rate
Dim EHP As Single 'EHP = Extra Hourly Percent
Dim myRow As Integer 'helping to paste output
Dim oRow As Integer '
Do Until IsEmpty(Cells(oRow, 1))
P = Cells(oRow, 3)
H = Cells(oRow, 4)
NS = 0
NL = 0
'Checking that there is enough people to continue
If P < 20 Then
MsgBox "The Minimum # of persons is 20", , "Sorry!"
NS = 0
NL = 0
BP = 0
OH = 0
OC = 0
TP = 0
Cells(oRow, 7) = NS
Cells(oRow, 8) = NL
Cells(oRow, 9) = BP
Cells(oRow, 10) = OH
Cells(oRow, 11) = OC
Cells(oRow, 12) = TP
Exit Do
ElseIf P > 120 Then
'checking that there are not to many participants.
MsgBox "Sorry the maximum amount of Persons is 120!", , "Sorry!"
NS = 0
NL = 0
BP = 0
OH = 0
OC = 0
TP = 0
Cells(oRow, 7) = NS
Cells(oRow, 8) = NL
Cells(oRow, 9) = BP
Cells(oRow, 10) = OH
Cells(oRow, 11) = OC
Cells(oRow, 12) = TP
Exit Do
'solving how many busses will be needed based on the amount of people
ElseIf P >= 20 And P <= 25 Then
NS = 1
ElseIf P >= 26 And P <= 50 Then
NS = 2
ElseIf P >= 51 And P <= 60 Then
NL = 1
ElseIf P >= 61 And P <= 85 Then
NS = 1
NL = 1
ElseIf P >= 86 And P <= 120 Then
NL = 2
End If
'calculate base price
BP = P * PPBR
'Calculating with no overtime
If H < 5 Then
TP = BP
OC = 0
OH = 0
Cells(oRow, 7) = NS
Cells(oRow, 8) = NL
Cells(oRow, 9) = BP
Cells(oRow, 10) = OH
Cells(oRow, 11) = OC
Cells(oRow, 12) = TP
'calculating overtime over 9 hours (which will only be 4 hours)
ElseIf H > 9 Then
OH = 4
OC = BP * OH * EHP
TP = OC + BP
Cells(oRow, 7) = NS
Cells(oRow, 8) = NL
Cells(oRow, 9) = BP
Cells(oRow, 10) = OH
Cells(oRow, 11) = OC
Cells(oRow, 12) = TP
'calculating overtime between 0 and 4 hours
Else
OH = H - 5
OC = BP * OH * EHP
TP = OC + BP
Cells(oRow, 7) = NS
Cells(oRow, 8) = NL
Cells(oRow, 9) = BP
Cells(oRow, 10) = OH
Cells(oRow, 11) = OC
Cells(oRow, 12) = TP
End If
oRow = oRow + 1
NS = 0
NL = 0
BP = 0
OH = 0
OC = 0
TP = 0
Loop
End Sub
VBA中此类问题的典型解决方案是重新构造代码,使计算部分与迭代单元格的部分位于单独的函数中。如果不满足正确的条件,则可以安全地退出计算函数,而不会影响迭代。您可以查看关键字Continue,或者以一种在不满足某些条件时不执行计算的方式构造代码。@David.C-您可能想到的是VB.Net。VBA没有
Continue
@BigBen我只是假设VBA有Continue关键字。谢谢你纠正我。嗯。。。是的,不幸的是,它没有:-(不过我同意结构评论)。