Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

Using Option Button to set a variable in excel vba

$
0
0

Hi I am relatively new to excel vba and I'm having some difficulty. I have a workbook with 3 sheets. The first sheet contains two option buttons and a button. When the user clicks the button I want to write a VBA script that will select the appropriate sheet (sheet 2 for option button 1, sheets 3 for option button 2), copy it to a new workbook and prompt the user to save the new workbook. My code is below:

Dim SheetName As String
Dim OptionButton1, OptionButton2 As OptionButton

If OptionButton1 = True Then Set SheetName = Sheet2Name
If OptionButton2 = True Then Set SheetName = Sheet3Name

Sheets(SheetName).Copy
If MsgBox("Would you like to save the new workbook?", vbYesNo _
    + vbQuestion, "") = vbYes Then
    Application.Dialogs(xlDialogSaveAs).show
End If

I am receiving a run-time error that says object variable not set on this line: If OptionButton2 = True Then Set SheetName = Sheet3Name. When I hover over the line, it says OptionButton2 = Nothing, but Optionbutton1 = Empty. I tried changing the variable declaration to :

Dim OptionButton1 As OptionButton
Dim OptionButton2 As OptionButton

but then both OptionButtons = Nothing. I feel like I am probably misunderstanding how to declare and set variables, but nothing I've tried has worked. Any help is appreciated.


Viewing all articles
Browse latest Browse all 88854

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>