modified code on userform

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hello
I need adding currency to textbox when populate values in textbox based on name optionbutton name
optionbutton1 is DL and optionbutton2 RMT name so when populate the values in textbox should like this

CR.PNG


VBA Code:
Option Explicit

Private Sub UserForm_Initialize()
    Me.ComboBox1.RowSource = ""
    With Sheets("sh1").Cells(1).CurrentRegion
        Me.ComboBox1.List = .Columns("b:e").Offset(1).Resize(.Rows.Count - 1).Value
    End With
End Sub

Private Sub OptionButton1_Click()
    If Me.OptionButton1 Then
        If Me.ComboBox1.ListIndex > -1 Then ComboBox1_Change
    End If
End Sub

Private Sub OptionButton2_Click()
    If Me.OptionButton2 Then
        If Me.ComboBox1.ListIndex > -1 Then ComboBox1_Change
    End If
End Sub

Private Sub ComboBox1_Change()
    Dim i As Long
    If Me.ComboBox1.ListIndex = -1 Then Exit Sub
    If (Not Me.OptionButton1) * (Not Me.OptionButton2) Then
        MsgBox "Select Option first", vbCritical: Exit Sub
    End If
    With Me.ComboBox1
        For i = 0 To 2
            Me("textbox" & i + 1) = Format$(.List(.ListIndex, i), "#,##0.00;;@")
            If (i = 2) * (Me.OptionButton2) Then
                Me("textbox" & i + 1) = Format$(.List(.ListIndex, i + 1), "#,##0.00")
            End If
        Next
        Me.TextBox4 = Format$(Val(.List(.ListIndex, 1)) * Val(.List(.ListIndex, 2 + IIf(Me.OptionButton1, 0, 1))), "#,##0.00")
    End With
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Change this line:
VBA Code:
Me.TextBox4 = Format$(Val(.List(.ListIndex, 1)) * Val(.List(.ListIndex, 2 + IIf(Me.OptionButton1, 0, 1))), "#,##0.00")

By these lines:
VBA Code:
dim prfix As String
prfix = IIf(OptionButton1, OptionButton1.Caption, OptionButton2.Caption)
Me.TextBox4 = prfix & " " & Format$(Val(.List(.ListIndex, 1)) * Val(.List(.ListIndex, 2 + IIf(Me.OptionButton1, 0, 1))), "#,##0.00")
 
Upvote 0
Replace ComboBox1_Change code for this:

VBA Code:
Private Sub ComboBox1_Change()
    Dim i As Long
    Dim prfix As String
    
    If Me.ComboBox1.ListIndex = -1 Then Exit Sub
    If (Not Me.OptionButton1) * (Not Me.OptionButton2) Then
        MsgBox "Select Option first", vbCritical: Exit Sub
    End If
    With Me.ComboBox1
      prfix = IIf(OptionButton1, OptionButton1.Caption, OptionButton2.Caption)
      Me.TextBox1 = Format$(.List(.ListIndex, 0), "#,##0.00;;@")
      Me.TextBox2 = Format$(.List(.ListIndex, 1), "#,##0.00;;@")
      Me.TextBox3 = prfix & " " & Format$(.List(.ListIndex, 2 + IIf(Me.OptionButton1, 0, 1)), "#,##0.00;;@")
      Me.TextBox4 = prfix & " " & Format$(Val(.List(.ListIndex, 1)) * Val(.List(.ListIndex, 2 + IIf(Me.OptionButton1, 0, 1))), "#,##0.00")
    End With
End Sub
 
Upvote 0
Solution
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,605
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top