Selected Answer
Try this:
Sub price_calc()
blind_type = Cells(Selection.Row, 1).Value
fabric_range = Cells(Selection.Row, 2).Value
qty = Cells(Selection.Row, 3).Value
fab_width = Cells(Selection.Row, 4).Value
fab_height = Cells(Selection.Row, 6).Value
If blind_type = "Crank" Then
If fabric_range = "Thames" Then
ws_name = "Crank-Op Thames"
ElseIf fabric_range = "Medway" Then
ws_name = "Crank-Op Medway"
End If
ElseIf blind_type = "Chain" Then
If fabric_range = "Thames" Then
ws_name = "Chain-Op Medway"
ElseIf fabric_range = "Medway" Then
ws_name = "Chain-Op Medway"
End If
End If
row_location = Application.WorksheetFunction.Match(fab_height, Sheets(ws_name).Range("A1:A16")) + 1
col_location = Application.WorksheetFunction.Match(fab_width, Sheets(ws_name).Range("A1:Q1")) + 1
unit_price = Sheets(ws_name).Cells(row_location, col_location).Value
Selection.Value = unit_price
End Sub
Most of the code is just to figure out which worksheet tab to use to get the info.
To use this, select the cell for which you want to get the unit price and then run the macro.
Update:
I updated the macro because it needed to be changed a bit in order for everything to work.
But, to get the macro to work for all instances, you need to adjust your data a little bit. You have to do this or the macro will have to get a bit more complex and difficult to maintain.
- Insert an empty row and empty column at the start of your pricing chart and place a 0 (zero) as the new "smallest" size.
- In the new empty space, type a formula that adds 1 to the current size.
- Reference the new cells in the macro, already done in the macro included above.
- Hide the new column/row of data by selecting it and making the text color white. You can also lock those cells if you want to keep them extra safe.
I did this for the "Chain-Op Thames" sheet and everything seems to be working well. Use that sheet as an example and do that for the other ones and it should work. File included below.
The other way to get around the issues is to reverse sort your pricing charts, which will look confusing, or beef up the macro a bit. Though, I think my method here is the easiest to maintain in the long-run.
(The data is already hidden in the Chain-Op Thames sheet, just select row 1 and column A and make the text black to see whats there.)