Add Userform caption value to specific range

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
Hi all

I have a simple Userform that contains 3 Option buttons, each one's "caption" is changed when the user form is opened to show the values held in 3 cells. These are dates.

MrExcel1.JPG


How do I add the Caption's value if selected to Sheet1 Range(A23) on closing the userform by clicking on a command button

For example if 25/02/2022 is selected this date will be copied and pasted into Sheet1 Range(A23
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Since your userform only contains 3 option buttons, try the following code . . .

VBA Code:
With Worksheets("Sheet1").Range("A23")
    If Me.OptionButton1.Value Then
        .Value = Me.OptionButton1.Caption
    ElseIf Me.OptionButton2.Value Then
        .Value = Me.OptionButton2.Caption
    ElseIf Me.OptionButton3.Value Then
        .Value = Me.OptionButton3.Caption
    End If
End With

Change the name of the option buttons accordingly.

Hope this helps!
 
Last edited:
Upvote 0
Solution
maybe
VBA Code:
Dim ctl As control

For Each ctl In UserForm1.Controls
   If TypeName(ctl) = "OptionButton" Then
      If ctl = -1 Then
         MsgBox ctl.Caption
         Sheets("Sheet1").Range("A23") = ctl.Caption
         Exit Sub
      End If
   End If
Next
I guess you could put that on form close event or a button click event - not sure what event you were looking for. Comment out or remove the msgbox line when/if you get it working.
 
Upvote 0
Since your userform only contains 3 option buttons, try the following code . . .

VBA Code:
With Worksheets("Sheet1").Range("A23")
    If Me.OptionButton1.Value Then
        .Value = Me.OptionButton1.Caption
    ElseIf Me.OptionButton2.Value Then
        .Value = Me.OptionButton2.Caption
    ElseIf Me.OptionButton3.Value Then
        .Value = Me.OptionButton3.Caption
    End If
End With

Change the name of the option buttons accordingly.

Hope this helps!

Thats great - many thanks ! :)
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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