Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

change currency based on option button and populate in textbox

0

hello

  i  have  this  file  when  i  select   one  of  them  option button  in  select  item  from  three comboboxes  it  gives  me  the  values   based on  column d, e   but  the  problem  it  gives  me  also  the  same  currancy   is $   what  i  would  when  i  select  option  button 1  it  change  the  currency  to  rm    so  the  number   becomes this  RM 1,200,340

thanks

Answer
Discuss

Answers

0
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.

  1. 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..
  2. Do the same for your TextBox1 which I renamed as TbxQty.
  3. 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.
  4. 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.
  5. Change your data on the "Brand" tab to a table.
    1. Name the table "Pricelist". If you use another name or prefer the generic "Table1" make sure that you update the references in the code.
    2. 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).
  6. 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.

Discuss

Discussion

thanks  variatus  but  i  tested  your  file  it  doesn't   show  any  currency beside  the  value    it  should   be  RM 120   OR  $ 40   it  just  shows   the  values  without  any  currency
leopard (rep: 88) Dec 19, '20 at 5:08 am
That's because you neither asked for it nor made preparations for the display of a currency in your form. Logically, the currency is shown by the option button. If you want to show it in TbxPrice in addition youi can add it while setting the display format. In fact, how to include a currency defined by a set of option buttons in the format for a price is atotally different question. If you want to ask it please don't add it on here. Prepare the question carefully and ask it separately.
Variatus (rep: 4889) Dec 19, '20 at 6:23 am
actually  i  have ever  made clear   that  above , i  quot    what  i  write  above  if  this  is  not  clear  you  don't  ask  me   make clear  more  the  subject   relates  showing  the  currency   [CODE]what  i  would  when  i  select  option  button 1  it  change  the  currency  to  rm    so  the  number   becomes this  RM 1,200,340
thanks  again
leopard (rep: 88) Dec 19, '20 at 6:44 am
Yes, I did misunderstand that part and have modified my answer to satisfy your request. Note, however, that data retrieval and data formatting are two different subjects.
Variatus (rep: 4889) Dec 19, '20 at 8:20 pm
thanks   very   much   for   this   assistance 
leopard (rep: 88) Dec 20, '20 at 2:23 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login