Passing a variable from a UserForm to a Procedure

Herve

Board Regular
Joined
May 6, 2003
Messages
68
Hello All!

I do not succeed to pass a variable (Control Button) from a UserForm to a Procedure (in a regular Module).

I am declaring the variable as Public and Static, but I get a message that VBA does not support this kind of automation.

What's the best way to do that? (I currently save the variable in a cell on a worksheet before unloading the UserForm, then read it from that location when I am back in the Procedure. I figure there must be an easier way :-)

Thanks!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Herve -

Not sure what you mean by a Control Button variable.

What I do is load the userform from the main procedure, but keep it hidden. I use code in the main procedure to initialize the userform, then finally use UserForm1.Show in the main code to show the form. The user can check checkboxes and enter text, etc. When finished, the button that the user clicks merely hides the form, and the main code can get whatever values it wants from the controls on the form. When it has harvested all the information it needs, then it unloads the form.

No global variables needed, and the main procedure remains in control the entire time.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______
 
Upvote 0
I also do what Jon does.

Sometimes If I'm using lot's of forms and collecting variables from here and there, I'll create an array and store the variables in the array (changing it's size as I go). I then pass this array to whatever subroutine is running when I call the subroutine. This allows me to close forms with crucial data. But perhaps I'm just a neat freak.
 
Upvote 0
Thanks Jon and Robert.
I must be doing something fundamentally wrong...

Let's say I am at a point in a procedure where I need the user to choose among ten values. I therefore load them in a ListBox in a UserForm (using an .AddItem method for instance), then I show the UserForm (UserForm.Show vbModal) which passes control to the UserForm.

The user selects Value(x) in the ListBox then clicks on the "OK" ControlButton1, whose code is:

Sub ControlButton1_Click()
User_Choice = ListBox.Value
Unload UserForm
End Sub

I am now back in my main procedure. How do I grab User_Choice which is a variable defined in the UserForm?

This is the question :-)

Thanks again for your time.
 
Upvote 0
Hi Herve,

Option1 - a Public variable (this must be in a general module, not the userform).

General Module:
Code:
Public User_Choice
'Declare Public variable in general module

Sub ShowUF()
UserForm1.Show
MsgBox User_Choice
End Sub
Userform code:
Code:
Private Sub CommandButton1_Click()
User_Choice = ListBox1.Value
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
With ListBox1
    .Clear
    .AddItem ("Test1")
    .AddItem ("Test2")
    .AddItem ("Test3")
End With
End Sub
Option2 - avoid use of Public variable (preferred).

General module:
Code:
Sub ShowUF()
UserForm1.Show
MsgBox UserForm1.ListBox1.Value
Unload UserForm1
'unload it now that info obtained
End Sub
Userform code:
Code:
Private Sub CommandButton1_Click()
UserForm1.Hide
'note Hide not Unload
End Sub

Private Sub UserForm_Initialize()
With ListBox1
    .Clear
    .AddItem ("Test1")
    .AddItem ("Test2")
    .AddItem ("Test3")
End With
End Sub
HTH
 
Upvote 0
Richie,

This is indeed the solution: UserForm1.Hide

Thanks a lot for for the detailed answer :-D
 
Upvote 0
Using this method, I need to use OptionButtons to know the choices selected, correct? (I am not talking about a list anymore.)

Is there a way to know if a CommandButton has been pressed (something like "If Button_Click is True" from the main code, the way I can read "If OptionButton is True" before unloading the UserForm as suggested above) ?

Reason: When the user can only select one choice, I think it's easier for him/her to press a single button rather than choose one option, then click on OK.)

To make myself clear, let's say I ask "Black" or "White."
I can use 2 OptionButtons and a CommandButton. Works fine. However, my preference so far has been to use 2 CommandButtons which save the choice AND close the UserForm at once. Obviously, as indicated first in this thread, I need to save the choice in a temporary cell.

Is there a way to do that?

Thanks!
 
Upvote 0
Hi Herve -

You can use code something like this in the userform:

Public TheColor as String

Private Sub CommandButtonBlack_Click()
TheColor = "Black"
Me.Hide
End Sub

Private Sub CommandButtonWhite_Click()
TheColor = "White"
Me.Hide
End Sub

And in the general code module:

Sub GetColorChoice()
Dim MyColor as String
UserForm1.Show
MyColor = UserForm1.TheColor
Unload UserForm1
MsgBox MyColor
End Sub

Of course, you still have the public variable, TheColor, in the userform. But you could replace this with a visible or invisible textbox on the form that stores the string "Black" or "White".

- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______
 
Upvote 0
Jon,

I love the invisible TextBox :diablo:

For those interested:

UserForm:

Private Sub Black_Click()
TextBox.Value = "Black"
Me.Hide
End Sub

Private Sub White_Click()
TextBox.Value = "White"
Me.Hide
End Sub

Main Code:

Sub GetColorChoice()
Dim MyColor As String
UserForm.Show
MyColor = UserForm.TextBox.Value
Unload UserForm
MsgBox MyColor
End Sub

Thanks a lot, this is very neat!
 
Upvote 0
Herve -

I use invisible text boxes and check boxes all the time. Very handy and very sneaky.

- Jon
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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