A simple if statement

Endeavouring

Board Regular
Joined
Jun 30, 2010
Messages
115
Hi
I am a novice to VBA and have the following problem. I have a ser form with one command button and 2 option buttons, I simply want the user to select one of the option buttons then depending on their selection run the related macro. What is wrong with the following as it coninually errors despite whatever changes I hae tried.

Private Sub CommandButton1_Click()
If OptionButton1.Value = True Then Sub1
ElseIf OptionButton2.Value = True Then Sub2
End If
End Sub

Thank you
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Have you tried this?

Code:
Private Sub CommandButton1_Click()
    If OptionButton1.Value = True Then
        Call Sub1
    ElseIf OptionButton2.Value = True Then
        Call Sub2
    End If
End Sub
 
Upvote 0
Cut and keep:-

How to code IF statements

1) Simple IF with a single statement
Code:
   If condition Then x = 1
2) Simple IF with multiple statements
Code:
   If condition Then x = 1: a = ""
3) Block IF..THEN
Code:
   If condition Then
     x = 1
     a = "xxx"
   End If
4) Block IF..THEN..ELSE
Code:
   If condition Then
     x = 1
     a = "xxx"
   Else
     x = 2
     a = "yyy"
   End If
5) Block IF..THEN..ELSEIF
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   End If
6) Block IF..THEN..ELSEIF..ELSE
Code:
   If condition1 Then
     x = 1
     a = "xxx"
   ElseIf condition2 Then
     x = 2
     a = "yyy"
   ElseIf condition3 Then
     x = 3
     a = "zzz"
   Else
     x = 4
     a = "xyz"
   End If

How to code SELECT statements

1) Single test variable
Code:
   Select Case variable
     Case 1, 3, 6 To 9
       x = 1
       a = "xxx"
     Case Is <= 100
       x = 2
       a = "yyy"
     Case Is <= 1000
       x = 3
       a = "zzz"
     Case Else
       x = 4
       a = "xyz"
   End Select
2) Multiple test variables
Code:
   Select Case True
     Case x < 100
       a = "xxx"
     Case a = "yyy", a = "zzz"
       x = 2
     Case Else
       x = 4
       a = "xyz"
   End Select
 
Upvote 0
Sorry, I meant the ammendment by Nightcrawler worked, but it leaves the original form with the command button open (named form1). I've tried unload in several places but that then errors. How can I close the original selection form (named form1) leaving just the form created by the sub1 or sub2 routines :(
 
Upvote 0
Try This.

Code:
Private Sub CommandButton1_Click()
     
    If OptionButton1.Value = True Then
         Unload Me
        Call Sub1
    ElseIf OptionButton2.Value = True Then
         Unload Me
        Call Sub2
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

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
Back
Top