I have a very large program that allows users to enter up to five quantities at once and the program returns a price for each quantity. I have the code structured so that the same code runs basically 5 times with one variable for each quantity. For example, take a look at the below code. I have several variables with the same name but a different number at the end.
That block of code repeats three more times.
Is there a way for me to simplify this code and consolidate it but using either arrays or a do loop? Or could I create a function for this and feed it the Qty variable and get back the price?
This is a simple block of the code but there are other sections that are much more complex. I'm looking to simplify this program so it is easier to maintain and if possible, improve run-time efficiency. My programming skills are pretty rudimentary so I'm looking for best practice on handling this case.
Code:
Qty1
If Qty1 <> 0 Then
Dim SlaggingTime1 As Single
SlaggingTime1 = SlaggingTimePerPiece * Qty1
Dim SlaggingAdminCharge1 As Single
SlaggingAdminCharge1 = (SlaggingAdminChargeRate * Me.NetWeight1.Value) / 100
If SlaggingAdminCharge1 < SlaggingAdminMinimum Then
SlaggingAdminCharge1 = SlaggingAdminMinimum
End If
Dim SlaggingCharge1 As Single
SlaggingCharge1 = (((SlaggingTime1 / 60) * SlaggingChargeRate) + SlaggingAdminCharge1) / Qty1
Me.SlaggingCost1.Visible = True
Me.SlaggingCost1.Value = SlaggingCharge1 * (1 - LaborDiscount)
Else
Me.SlaggingCost1.Value = 0
Me.SlaggingCost1.Visible = False
End If
' Qty2
If Qty2 <> 0 Then
Dim SlaggingTime2 As Single
SlaggingTime2 = SlaggingTimePerPiece * Qty2
Dim SlaggingAdminCharge2 As Single
SlaggingAdminCharge2 = (SlaggingAdminChargeRate * Me.NetWeight2.Value) / 100
If SlaggingAdminCharge2 < SlaggingAdminMinimum Then
SlaggingAdminCharge2 = SlaggingAdminMinimum
End If
Dim SlaggingCharge2 As Single
SlaggingCharge2 = (((SlaggingTime2 / 60) * SlaggingChargeRate) + SlaggingAdminCharge2) / Qty2
Me.SlaggingCost2.Visible = True
Me.SlaggingCost2.Value = SlaggingCharge2 * (1 - LaborDiscount)
Else
Me.SlaggingCost2.Value = 0
Me.SlaggingCost2.Visible = False
End If
That block of code repeats three more times.
Is there a way for me to simplify this code and consolidate it but using either arrays or a do loop? Or could I create a function for this and feed it the Qty variable and get back the price?
This is a simple block of the code but there are other sections that are much more complex. I'm looking to simplify this program so it is easier to maintain and if possible, improve run-time efficiency. My programming skills are pretty rudimentary so I'm looking for best practice on handling this case.
Last edited: