ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a form with option buttons (https://www.excelbanter.com/excel-programming/358874-creating-form-option-buttons.html)

Jon[_21_]

Creating a form with option buttons
 
I need to create a user form that has 4 option buttons and an ok button
to close the form. i have gotten the ok button to work fine with

Private Sub OK_Click()
Unload UserForm1
End Sub

but now i want to make the 4 option buttons set a variable to either
1,2,3, or 4. How can I do this? Do i need code in the private sub for
the button or in my main code? Thanks!


Norman Jones

Creating a form with option buttons
 
Hi Jon,

In a standard module, before any code, paste:

Public myVar As Long

Then, in the userform module, try:

'=============
Private Sub OptionButton1_Click()
myVar = 1
End Sub

Private Sub OptionButton2_Click()
myVar = 2
End Sub

Private Sub OptionButton3_Click()
myVar = 3
End Sub

Private Sub OptionButton4_Click()
myVar = 4
End Sub
'<<=============

If you wish to reset the myVar variable each time that the form is opened,
try:

'=============
Private Sub UserForm_Initialize()
myVar = 0
End Sub
'<<=============

This should be pasted in the userform module.


---
Regards,
Norman


"Jon" wrote in message
oups.com...
I need to create a user form that has 4 option buttons and an ok button
to close the form. i have gotten the ok button to work fine with

Private Sub OK_Click()
Unload UserForm1
End Sub

but now i want to make the 4 option buttons set a variable to either
1,2,3, or 4. How can I do this? Do i need code in the private sub for
the button or in my main code? Thanks!




Tom Ogilvy

Creating a form with option buttons
 
Without knowing more, possibly:

Public v as Integer
Private Sub OptionButton1_Click()
v = 1
End Sub

Private Sub OptionButton2_Click()
v = 2
End Sub

Private Sub OptionButton3_Click()
v = 3
End Sub

Private Sub OptionButton4_Click()
v = 4
End Sub

--
Regards,
Tom Ogilvy


"Jon" wrote in message
oups.com...
I need to create a user form that has 4 option buttons and an ok button
to close the form. i have gotten the ok button to work fine with

Private Sub OK_Click()
Unload UserForm1
End Sub

but now i want to make the 4 option buttons set a variable to either
1,2,3, or 4. How can I do this? Do i need code in the private sub for
the button or in my main code? Thanks!




Jon[_21_]

Creating a form with option buttons
 
Thanks. My error was that I did not define the variable as public.



All times are GMT +1. The time now is 03:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com