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

Using Option Button to set a variable

$
0
0

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 OptionButton2is Nothing, but Optionbutton1 is Empty. I tried changing the variable declaration to:

Dim OptionButton1 As OptionButton
Dim OptionButton2 As OptionButton

but then both OptionButtons are Nothing.


Viewing all articles
Browse latest Browse all 88075

Trending Articles



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