VBA Option Button that selects spreadsheet

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
I have created a user form that looks like the following:

1. Enter Name....

2. Enter Telephone Number....

3. Enter Address.....

4. Select Department..... (from option buttons)

- Primary
- Secondary
- Accounts
- Overseas
- Associations

The department selection will determine which sheet to open and enter the rest of the information into the next blank lines in the corresponding colums, i.e. Name in A, Telephone Number in B, Address in C.

Is this possiable using the process/layout I have suggested and what would be the correct code to use?

Thanks for taking the time to read this and any advise you can give :)
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
lhk201,

Assuming your form has a commandbutton that is clicked once everything is filled in, you could use the following:
Code:
Private Sub CommandButton1_Click()
    
    Dim nRow As Long
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "OptionButton" Then
            If ctrl.Value = "True" Then
                With Sheets(ctrl.Caption)
                    nRow = .Cells(Rows.Count).End(xlUp).Row + 1
                    .Cells(nRow, "A").Value = Trim(Me.TextBox1.Text)
                    .Cells(nRow, "B").Value = Trim(Me.TextBox2.Text)
                    .Cells(nRow, "C").Value = Trim(Me.TextBox3.Text)
                    Exit Sub
                End With
            End If
        End If
    Next ctrl
    
    MsgBox "Must select a department"
    
End Sub



Hope that helps,
~tigeravatar
 
Upvote 0
Thank you for your reply!

I couldn't quite get this to work. What should I be changing to adapt it to my document.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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