Joshlindsay
Registered User.
- Local time
- Today, 14:34
- Joined
- Jun 6, 2012
- Messages
- 17
Price calculation loop driven from table. Is this possible?
I've written a chunk of code which calculates an average price based upon a quantity - I'm pretty stoked with it being a complete novice.
Basically the first 50 items are charged at a certain price, the next 50 at a certain price, the next 100 and a certain price all the way to to 5000 with different quantity breaks.
This will be being used as part of an estimating system for a printing company.
Ideally I would like the rates and quantity breaks to be driven from a table so additions and changes could be made without touching the code.
However that won't work with each quantity being hard coded as I've done below.
The above code has been simplified for posting as it is quite long and just repeats.
Could it be achieved with a super clever loop?
If you had any tips or advise on where I could start looking to achieve my idea scenario would be awesome.
Thanks
I've written a chunk of code which calculates an average price based upon a quantity - I'm pretty stoked with it being a complete novice.
Basically the first 50 items are charged at a certain price, the next 50 at a certain price, the next 100 and a certain price all the way to to 5000 with different quantity breaks.
This will be being used as part of an estimating system for a printing company.
Ideally I would like the rates and quantity breaks to be driven from a table so additions and changes could be made without touching the code.
However that won't work with each quantity being hard coded as I've done below.
Code:
Dim Remaining As Integer
Dim TotalQuantity As Integer
Dim TotalPrice As Currency
Dim AvgPrice As Currency
'Rates
rate50 = 2
rate100 = 1.9
rate150 = 1.8
rate200 = 1.7
'setQuantity
TotalQuantity = 140
'CalculatesPrice
If TotalQuantity < 51 Then
TotalPrice = TotalQuantity * rate50
AvgPrice = TotalPrice / TotalQuantity
End If
If TotalQuantity > 51 And TotalQuantity < 101 Then
Remaining = TotalQuantity - 50
TotalPrice = (50 * rate50) + Remaining * rate100
AvgPrice = TotalPrice / TotalQuantity
End If
If TotalClicks > 101 And TotalClicks < 151 Then
Remaining = TotalClicks - 100
TotalPrice = (50 * rate50) + (50 * rate100) + Remaining * rate150
AvgPrice = TotalPrice / TotalQuantity
End If
'This continues on to 5000
MsgBox ("Total price for " & TotalQuantity & " items is $" & TotalPrice & ". The average price is $" & AvgPrice)
Could it be achieved with a super clever loop?
If you had any tips or advise on where I could start looking to achieve my idea scenario would be awesome.
Thanks
Last edited: