I have a workbook with three sheets.
The first sheet contains two option buttons and a button. When the user clicks the button I want to copy the appropriate sheet (sheet 2 for option button 1, sheets 3 for option button 2), to a new workbook and prompt the user to save the new workbook.
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 receive the run-time error
object variable not set
on this line:
If OptionButton2 = True Then Set SheetName = Sheet3Name
When I hover over the line, it says OptionButton2
is Nothing
, but Optionbutton1
is Empty
. I tried changing the variable declaration to:
Dim OptionButton1 As OptionButton
Dim OptionButton2 As OptionButton
but then both OptionButton
s are Nothing
.