For some reasons I want to calculate how much a price will be, compared to the weight of an item.
I've created two columns in excel, what I actually want is that if I input in A1 the amount of LBS, for ex 100, the output need to be $150,- in B1. I've tried something with a simple VBA code. IT works but, the prices changes overtime.
In column C (LBS) and D (price), I've written down the lbs and prices (in sheet 2). For ex
LBS Price
100 150
200 300
300 450
I've the following code:
Private Sub CommandButton1_Click()
Dim kilo As Integer, result As String
kilo = Range("A1").Value
Select Case kilo
Case Is >= 300
result = "450 euro"
Case Is >= 200
result = "300 euro"
Case Is >= 1
result = "150 euro"
Case Else
result = "nvt"
End Select
Range("B1").Value = result
End Sub
The problem is the list is too long, it goes to 40000 LBS. Above the 40.000 LBS/Weight there comes a price of xxx per 500 lbs/Kilo
the formula is like this between 100 and 199 lbs, the price will be € 300,- Between 200 and 299 price will be € 450,-
Does anyone have an idea? I want to result the cell, so if the weight is between 200 and 300, the output should been € 450,- cell (B4)
Thank you very much.
I'm not too clear on your data structure here, but the following should help to move you in the right direction:
With your list of LBS | Price in Sheet 2 (columns C and D), ensuring that list is sorted in ascending order ...
=VLOOKUP(A1,Sheet2!C:D,2,True)
This formula "looks up" the value from the cell A1
in the first column of the range Sheet2!C:D
. Where no match is found, by using True
as the final parameter, we treat the (ordered) list as a set of thresholds ... the last threshold which is met or exceeded will be considered the "matched" row. The third parameter 2
, means we retrieve the second within the range Sheet2!C:D
.
You can find the Office Support page for VLOOKUP
here
Note, this will not handle your "Max" without some further work. You could for example use this as input for a second formula something like:
=If([output] >= [Max], [Per LB Calculation], [output])
I'm not so clear what is the maximum price, but you can use the formula below to calculate the PriceFactor
per "LBS" range:
PriceFactor = WorksheetFunction.RoundUp((Kilo / 100), 0)
and then multiply PriceFactor * 150
to get the Result
.
Code
Option Explicit
Private Sub CommandButton1_Click()
Dim Kilo As Long, Result As Long
Dim PriceFactor As Double
Kilo = Range("A1").Value
PriceFactor = WorksheetFunction.RoundUp((Kilo / 100), 0)
Result = PriceFactor * 150
Range("B1").Value = Result
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.