Sie sind auf Seite 1von 4

4/7/2015

Excel VBA and Option Buttons

Excel VBA Programming


Home
Getting Started
8 part section >>
VBA Programming Variables
6 Part Section >>
Conditional Logic
9 part section >>
Strings and String Functions
8 Part Section >>
Programming Loops
4 Part Section >>
Programming Arrays
4 Part Section >>
Subs and Functions
6 Part Section >>
Excel VBA and Text Files
2 Part Section >>
Excel VBA and User Forms
5 part section >>
An Excel Picture Viewer Project
Excel Picture Viewer
Design the User Form
Option and Command Buttons
Add New Photo Tab
Form Initialize Event
Get Textbox data
VBA Option Button Code
ImageBox VBA Code
Next and Previous Buttons
Add New Photo Button
Save New Image
Copy an Image with Excel VBA
Excel VBA and Charts
4 part section >>
A TreeView Project
A 6 part section >>

http://www.homeandlearn.org/code_for_vba_option_buttons.html

1/4

4/7/2015

Excel VBA and Option Buttons

Coding For Option Buttons in VBA


Ongoing tutorial - First part is here: Part One
Option buttons tend to work as a group. When one button is selected the other (or others) become
deselected.

Excel Data Analysis


5 Ways to Enhance Excel Data. Download the Free Whitepaper!

So that your buttons are treated as a group, return to your form in the VBA Editor and click on the YES
option button. Now have a look at the properties for the button. Locate the GroupName property and
type OB1. Press the enter key on your keyboard and do the same for the NO option button. Both
buttons will then have a value of OB1 for their GroupName property (OB1 is just something we came
up with - you can call your group of option buttons anything you like.):

Go back to your coding window and create a new Private Sub. Call it GetOptionButtonValue. What
we need to do, here, is to get the value from Column 7 on our spreadsheet (the G Column). We'll then
use an If Statement to test this value. Here's the full code for this new Sub:
http://www.homeandlearn.org/code_for_vba_option_buttons.html

2/4

4/7/2015

Excel VBA and Option Buttons

Private Sub GetOptionButtonValue( )


Dim OB As Variant
OB = ActiveCell.Offset(, 6).Value
If OB = "Yes" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
End Sub
We've set up a Variant variable and called it OB. The second line gets the value from the spreadsheet:
OB = ActiveCell.Offset(, 6).Value
The active cell is in column 1, remember, so we use Offset to move 6 columns over into the G
Column. We then get the Value of this cell.
The If Statement checks what is inside of the OB variable:
If OB = "Yes" Then
OptionButton1.Value = True
Else
OptionButton2.Value = True
End If
If the value is "Yes" then we set the Value property of OptionButton1 to True. If it's not "Yes" then we
set the value of OptionButton2 to True. This is enough to either switch on an Option Button or switch
it off.
We now need to call this new Sub into action. Click inside of your cmdLoad code and add the
following line just before End Sub:
Call GetOptionButtonValue
Your code will then look like this:

http://www.homeandlearn.org/code_for_vba_option_buttons.html

3/4

4/7/2015

Excel VBA and Option Buttons

You can test it out again. Run your form and click the Load Image Information button. You should find
that the correct option button is selected for the Flash item.
We can now load an image into the image box. We'll do that in the next lesson below.

Load an Image into an Image Box >


Lots more free online course here on our main Home and Learn site
All course material copyright Ken Carney

http://www.homeandlearn.org/code_for_vba_option_buttons.html

4/4

Das könnte Ihnen auch gefallen