Option button to select a worksheet

zookeepertx

Well-known Member
Joined
May 27, 2011
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hello all!

This is driving me crazy! All I want to do is have my macro open an existing workbook and call up a Userform with 2 option buttons so the user can select which of the 2 sheets in that workbook the rest of the macro will apply to.
It seems like it should be so easy, but I've been working on it most of yesterday afternoon and most of today so far and I don't seem to be any closer than when I started! Here's the (totally failed) code I have so far:
VBA Code:
Private Sub OptionButton1_Click()

If Me.OptionButton1.Value = Then Sheets("AB").Select
ElseIf OptionButton2.Value = True Then Worksheets("CD").Select
End If

Unload Me
End Sub

(The 2 lines are different because I keep trying different things on the first one, so it's already had a lot of different wording from the 2nd line as each attempt fails)
Right now the 1st line - the OptionButton1.Value line - is failing at "Then" and saying "Expected Expression". And that happens without even trying to RUN the thing! When I type that, it immediately gives the error message.

Please somebody help! I have the ENTIRE rest of the macro ready to hand over to my coworker if this part will just work. (I wrote the rest of it first because, so save time, I just wrote it to work with 2 sheets in the same workbook. Now I'm trying to make it recognize the ACTUAL workbooks/worksheets it needs to work on.)

I should say that this code is very short because I thought I'd just get the option buttons doing what they need to do, then put that bit of code into the existing macro. This may or may not be a good approach; feel free to correct me if it's a stupid idea.

Thanks!
Jenny
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,515
Office Version
  1. 2019
Platform
  1. Windows
Thanks so much for your help so far!
If I mark your first post at the start as "the solution" will I be able to mark a future addition to the code as a solution too? Or can you only mark 1 per topic? (Just want to be sure to give credit for a combination of answers that make up the TOTAL answer, LOL!)

Jenny
That's very considerate of you & much appreciated
Solution marking is not a feature I have used so not sure if you can mark more than one in same thread however, you probably will want to start a new thread at later stage if your requirement is for a different reason as this will likely attract more responses.

Good luck with project

Dave
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

zookeepertx

Well-known Member
Joined
May 27, 2011
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Thank you very much for your help! Have a great weekend.

Jenny
 

Watch MrExcel Video

Forum statistics

Threads
1,129,795
Messages
5,638,370
Members
417,023
Latest member
Zimbo38

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
Top