Multiple OptionButtons - Cell updated with selection

Watchdawg

Board Regular
Joined
Jan 21, 2015
Messages
84
Good morning all. I hope that I'm putting this in the correct place, I've looked and some of the answers I've found answer part of the issue but not all. I have a userform. When the user double-clicks the "Vendor" cell in the workbook, it pulls a userform with 28 vendors attached to option buttons. There can be only one vendor per cell, which is exactly how I need it. My problem is when an optionbutton is selected, how do I make it automatically populate the ActiveCell with the .Caption from the option button and close the UserForm? I know that I can manually attach code to each and every optionButton to perform this action, but that's amazingly tedious and very messy for code.
Here's the code that I can attach to each button (although this is probably pointless for the folks here)
VBA Code:
Private Sub OptionButton1_Click()
    If OptionButton1.Value = True Then
        ActiveCell.Value = OptionButton1.Caption
    End If
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Rather than having 28 option buttons, why not use a listbox, or combobox?
 
Upvote 0
That was my original thought as well, Fluff. That said, this is what the boss prefers so this is what I'm trying to make work! LOL
 
Upvote 0
Would you settle for a command button to push the data to the sheet?
 
Upvote 0
In that case try
VBA Code:
Private Sub CommandButton1_Click()
    Dim Ctrl As MSForms.Control
    
    For Each Ctrl In Me.Controls
        If TypeOf Ctrl Is MSForms.OptionButton Then
            If Ctrl Then ActiveCell.Value = Ctrl.Caption
        End If
    Next Ctrl
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Of course, I love it when I come here, I'm always learning a new / better way to do something with VBA!
 
Upvote 0
Another way of doing it, without the command button is:-
Create a new class module & call it "ClsOptBtn" then add this code
Code:
Option Explicit
Public WithEvents Optbtn As MSForms.OptionButton
Private Sub OptBtn_Click()
    ActiveCell.Value = Optbtn.Caption
End Sub
Then in the Userform module put
Rich (BB code):
Option Explicit
Private OptBtnEvent As Collection

Private Sub UserForm_Initialize()
    Dim Ctrl As MSForms.Control
    Dim Obtn As ClsOptBtn
   
    Set OptBtnEvent = New Collection
    For Each Ctrl In Me.Controls
        If TypeOf Ctrl Is MSForms.OptionButton Then
            Set Obtn = New ClsOptBtn
            Set Obtn.Optbtn = Ctrl
            OptBtnEvent.Add Obtn
        End If
    Next
End Sub
Ensuring the two lines in blue are at the very top of the module, before any code
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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