Pass variable to userform 
Author Message
 Pass variable to userform

Is it possible to pass a variable from my main VBA module to the userform
code?  If so, how is it done?

Barney Byrd



Wed, 19 Oct 2005 18:35:36 GMT  
 Pass variable to userform
Barney,

There are ways to programmatically "pass" a variable but the
easiest thing to do is to declare a Public variable at the top
of a regular module and it'll be available to all other
modules, userforms and event procedures.

Ex:
At the top of a regular module
Public myString as String

John

Quote:

> Is it possible to pass a variable from my main VBA module to the userform
> code?  If so, how is it done?

> Barney Byrd




Wed, 19 Oct 2005 18:53:31 GMT  
 Pass variable to userform
Hi Barney

Allow me to disagree with John; ease of programming tend to clog up the app with tons of public variables available everywhere,
belonging nowhere and useful one or two secret places only.

Put this in the userform module of Userform1 (containing a Textbox1):

Public Sub FillText(strWhatText As String)
TextBox1.Text = strWhatText
End Sub

They keyword here is "Public". Now put this in a standard module and run it:

Sub test()
Call UserForm1.FillText("Hi Barney!")
UserForm1.Show
End Sub

--
HTH. Best wishes Harald
Excel MVP

Followup to newsgroup only please.

Quote:

> Is it possible to pass a variable from my main VBA module to the userform
> code?  If so, how is it done?

> Barney Byrd




Wed, 19 Oct 2005 19:15:09 GMT  
 Pass variable to userform
How about using Property Set in the form. In this example the variable is
private to the userform.

'Create a form (UserForm1) and a command button (CommandButton1)

Private rng As Range

Public Property Set MyRng(ByVal rngVal As Range)
    Set rng = rngVal
End Property

Private Sub CommandButton1_Click()
    MsgBox "The range passed is " & rng.Address
End Sub

'and in a module:

Sub Tester()
    Set UserForm1.MyRng = ActiveSheet.Range("A1:D10")
    UserForm1.Show
End Sub


Quote:

> Is it possible to pass a variable from my main VBA module to the userform
> code?  If so, how is it done?

> Barney Byrd




Wed, 19 Oct 2005 19:20:42 GMT  
 Pass variable to userform
You can pass a variable from the main module directly to the value in a form
by:
frmUserForm.variablename.Value = MainModuleValue

Michael

Quote:

> Is it possible to pass a variable from my main VBA module to the userform
> code?  If so, how is it done?

> Barney Byrd




Wed, 19 Oct 2005 21:30:08 GMT  
 Pass variable to userform
Harald,

You're certainly allowed to disagree as long as I'm allowed
to learn from yours (or anyone else's) corrections.

I guess that I've gotten into the habit (albeit, maybe a bad one)
of setting up a separate module for all my Public variables.
At least there I can comment them (so I know what secret
places I used them in) and I'll always know where to find them
(in case I need to modify or delete them).

So far, I haven't had any problems (that I know of yet) with
declaring maybe 50 to 100 of them for a project.

Agreed that my way is clunky and not the best way to do
it (as it can tie up memory) but if you're only going to use
a few of them in a project, why not just make them Public?
(It's a lot less coding and easier to debug)

John

Quote:

> Hi Barney

> Allow me to disagree with John; ease of programming tend to clog up the app with tons of public variables available everywhere,
> belonging nowhere and useful one or two secret places only.

> Put this in the userform module of Userform1 (containing a Textbox1):

> Public Sub FillText(strWhatText As String)
> TextBox1.Text = strWhatText
> End Sub

> They keyword here is "Public". Now put this in a standard module and run it:

> Sub test()
> Call UserForm1.FillText("Hi Barney!")
> UserForm1.Show
> End Sub

> --
> HTH. Best wishes Harald
> Excel MVP



Wed, 19 Oct 2005 21:43:31 GMT  
 Pass variable to userform

Thanks to all who responded to my question.

I decided to adopt Harald's solution as it best fit my needs.

This newsgroup is a wonderful resource for struggling amateur programmers
like myself.

Barney Byrd


Quote:

> Is it possible to pass a variable from my main VBA module to the
> userform code?  If so, how is it done?

> Barney Byrd




Thu, 20 Oct 2005 08:36:40 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Passing variables between userforms and/or normal procedures

2. Excel 97: Pass variable from userform to Sub ?

3. Passing Variables between UserForm/Module

4. Passing a Variable from one Userform to another Userform

5. Passing a variable through a UserForm

6. Passing variables or arrays to userforms

7. Passing variables or arrays to userform objects

8. Passing UserForm Variable To Class Module

9. Passing a variable through a UserForm

10. Passing strings/variables between userform and macro


 
Powered by phpBB® Forum Software © phpBB Group