Selected Answer
I got good news for you and bad news. I'll give you the good news first. Here's the code you've been looking for. Just replace the procedure of the same name in your project with this one.
Private Sub GetPrice()
' 143 - 20 Dec 2020
Dim Fnd As Range ' found range by Find
Dim FirstFnd As Long ' first instance found
If Len(CbxBrand.Value) Then
With Worksheets("Brand").ListObjects("Pricelist").DataBodyRange
With .Columns(1) ' search column A
Set Fnd = .Find(What:=CbxBrand.Value, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchDirection:=xlNext, _
MatchCase:=False)
End With
If Not Fnd Is Nothing Then
FirstFnd = Fnd.Row
Do
If Fnd.Offset(0, 1).Value = CbxType.Value Then
If Fnd.Offset(0, 2).Value = CbxOrigin.Value Then
TbxPrice.Value = Split("RM USD")(Abs(OptUSD.Value)) & _
Format(Fnd.Offset(0, 4 + Int(OptUSD.Value)).Value, _
" #,##0.00")
Exit Do
End If
End If
Set Fnd = .FindNext(Fnd)
If Fnd Is Nothing Then Exit Do
Loop While Fnd.Row <> FirstFnd
End If
End With
End If
End Sub
But now the bad news. The code won't work for you. Take the following steps to make it work.
- Rename your 3 combo boxes as CbxBrand, CbxType and CbxOrigin.
As you rename them the references in your existing code should change automatically. If they don't, use Edit/Replace to update the references..
- Do the same for your TextBox1 which I renamed as TbxQty.
- Rename your two option buttons as OptRM and OptUSD. Run the appropriate Edit/Replace on the code to get rid of the names OptionButton1 and OptionButton2. When I made the change in my copy of your project I found that the event procedure names weren't changed to match. You can change them individually but Edit/Replace will take care of it faster.
- The two option buttons must be joined in a group so that one will get de-selected when the other one is clicked. Find the GroupName property and give the group a name. I suggest "Currenciy" (type without the quotation marks). Same group name for both buttons, of course.
- Change your data on the "Brand" tab to a table.
- Name the table "Pricelist". If you use another name or prefer the generic "Table1" make sure that you update the references in the code.
- Observe that the tab is referenced in the code as Worksheets("Brands"). If this isn't the real name make sure that the real name is used in the code (or use the CodeName).
- Add a textbox to your form and name it as TbxPrice. The above code outputs the price to this textbox. An error will occur if it doesn't exist.
Finally, note that I removed code to erase the value of TbxQty in the GetPrice procedure. There is no good reason to forget the quantity when the price is set. On the contrary, if you want to have a "value" field in the future which shows Price*Qty its value should be calculated here. Note that the price is shown as a text string which you can't use to calculate. Therefore any calculation should be done with the value retrieved from the table before it is changed to text.
I hope I didn't forget to mention anything. Just in case, I attach a copy of my workbook for your reference.