Option Buttons
Form Controls
- To reference a member of the Worksheets collection, you need to use
Worksheets(...) as mentioned by braX in your comments. To see the difference, in a sub write Worksheet(. Nothing will happen. Remove or out-comment the line. Then try Worksheets( and you will see that IntelliSense kicks in.
- Shapes don't have a
Value property. In a sub, write the line Dim shp As Shape: shp.v and observe how it isn't offered by IntelliSense.
- When selected, an Option Button's value is
1 (not True). Otherwise, it's -4146 (not False).
A Quick Fix
Sub GenerateEmail_Click_QF()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Calculator")
If ws.OptionButtons("Option Button 1").Value = 1 Then
Email_No_New
ElseIf ws.OptionButtons("Option Button 2").Value = 1 Then
Email_New
Else
MsgBox "No option button selected!", vbExclamation
End If
End Sub
An Improvement
- A With statement often makes the code more readable. A Select Case statement is often more appropriate than using (multiple)
ElseIf statements (not so much in this case).
Sub GenerateEmail_Click()
With ThisWorkbook.Sheets("Calculator")
Select Case 1 ' 1 (selected); -4146 (not selected)
Case .OptionButtons("Option Button 1").Value: Email_No_New
Case .OptionButtons("Option Button 2").Value: Email_New
Case Else: MsgBox "No option button selected!", vbExclamation
End Select
End With
End Sub
ActiveX Controls
- If you decide to use ActiveX controls instead of Form controls, the same would look like this:
Sub GenerateEmail_Click_ActiveX()
With ThisWorkbook.Sheets("Calculator")
Select Case True ' True (selected); False (not selected)
Case .OLEObjects("OptionButton1").Object.Value: Email_No_New
Case .OLEObjects("OptionButton2").Object.Value: Email_New
Case Else: MsgBox "No option button selected!", vbExclamation
End Select
End With
End Sub
- The values of these controls are
True or False. Also, note how their generic names are different (no spaces).
Worksheets("Calculator")(plural) - notWorksheet("Calculator")