Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Passing info from macro to UserForm

What is the best way to get data that I have in a macro to a UserForm? I
want a variable in MacroA to be available to UserFormA. From best practices,
I know that global variables aren't the best solution. What would be the
recommended practice to get that data in MacroA to UserFormA?

Thanks,

J
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Passing info from macro to UserForm

UserFormA.Tag = CStr(MyVariable)
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"ybazizi"
wrote in message...
What is the best way to get data that I have in a macro to a UserForm? I
want a variable in MacroA to be available to UserFormA. From best practices,
I know that global variables aren't the best solution. What would be the
recommended practice to get that data in MacroA to UserFormA?
Thanks,
J
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Passing info from macro to UserForm

J,

One way is to create a property in the UserForm using a Let statement. The
book Professional Excel Development covers this well.

Doug

"ybazizi" wrote in message
...
What is the best way to get data that I have in a macro to a UserForm? I
want a variable in MacroA to be available to UserFormA. From best
practices,
I know that global variables aren't the best solution. What would be the
recommended practice to get that data in MacroA to UserFormA?

Thanks,

J



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Passing info from macro to UserForm

J,

Here's an example, which I developed from the book I already mentioned.

You need a UserForm1 with the following code. You'll have to name the two
CommandButtons as shown:

Private Sub UserForm_Activate()
Call initialize_controls(data_in)
End Sub

Property Let data_on_form(passed_data As String)
data_in = passed_data
End Property

Property Get data_on_form() As String
data_in = TextBox1.Value
data_on_form = data_in
End Property

Sub initialize_controls(data_in As String)
TextBox1.Value = data_in
End Sub

Private Sub OKButton_Click()
'for Closed with OK button property
OK_clicked = True
Me.Hide
End Sub

Property Get closed_with_OK()
'this property tells a calling procedure whether the OK button was clicked
closed_with_OK = OK_clicked
End Property

Private Sub CancelButton_Click()
Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'if closed with x it calls the Cancel routine,
'so form is hidden, not unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
Call CancelButton_Click
End Sub

Then in a regular module paste this code.

Sub pass_to_uform()
Dim uform_instance As UserForm1
Dim data_to_pass As String

data_to_pass = "widget A"
Set uform_instance = UserForm1
'pass the events from the registry to the form
uform_instance.data_on_form = data_to_pass
uform_instance.Show
'only do if the user chose OK
If uform_instance.closed_with_OK Then
'fill the form events array, with those from the form
data_to_pass = uform_instance.data_on_form
End If
Unload uform_instance

MsgBox data_to_pass

End Sub

Run the code. You'll see the original value "Widget A" in the TextBox.
Change the value and click OK. The form closes and the MsgBox displays your
changed value. Again, all from PED.

hth,

"Doug Glancy" wrote in message
...
J,

One way is to create a property in the UserForm using a Let statement.
The book Professional Excel Development covers this well.

Doug

"ybazizi" wrote in message
...
What is the best way to get data that I have in a macro to a UserForm? I
want a variable in MacroA to be available to UserFormA. From best
practices,
I know that global variables aren't the best solution. What would be the
recommended practice to get that data in MacroA to UserFormA?

Thanks,

J





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Passing info from macro to UserForm

Whoops, I missed some code with my paste. This goes at the very top of the
UserForm, before Private Sub UserForm_Activate():

Private data_in As String
'global variable for property whether closed with the OK button
Private OK_clicked As Boolean

hth,

Doug

"Doug Glancy" wrote in message
...
J,

Here's an example, which I developed from the book I already mentioned.

You need a UserForm1 with the following code. You'll have to name the two
CommandButtons as shown:

Private Sub UserForm_Activate()
Call initialize_controls(data_in)
End Sub

Property Let data_on_form(passed_data As String)
data_in = passed_data
End Property

Property Get data_on_form() As String
data_in = TextBox1.Value
data_on_form = data_in
End Property

Sub initialize_controls(data_in As String)
TextBox1.Value = data_in
End Sub

Private Sub OKButton_Click()
'for Closed with OK button property
OK_clicked = True
Me.Hide
End Sub

Property Get closed_with_OK()
'this property tells a calling procedure whether the OK button was clicked
closed_with_OK = OK_clicked
End Property

Private Sub CancelButton_Click()
Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
'if closed with x it calls the Cancel routine,
'so form is hidden, not unloaded
If CloseMode = vbFormControlMenu Then Cancel = True
Call CancelButton_Click
End Sub

Then in a regular module paste this code.

Sub pass_to_uform()
Dim uform_instance As UserForm1
Dim data_to_pass As String

data_to_pass = "widget A"
Set uform_instance = UserForm1
'pass the events from the registry to the form
uform_instance.data_on_form = data_to_pass
uform_instance.Show
'only do if the user chose OK
If uform_instance.closed_with_OK Then
'fill the form events array, with those from the form
data_to_pass = uform_instance.data_on_form
End If
Unload uform_instance

MsgBox data_to_pass

End Sub

Run the code. You'll see the original value "Widget A" in the TextBox.
Change the value and click OK. The form closes and the MsgBox displays
your changed value. Again, all from PED.

hth,

"Doug Glancy" wrote in message
...
J,

One way is to create a property in the UserForm using a Let statement.
The book Professional Excel Development covers this well.

Doug

"ybazizi" wrote in message
...
What is the best way to get data that I have in a macro to a UserForm?
I
want a variable in MacroA to be available to UserFormA. From best
practices,
I know that global variables aren't the best solution. What would be
the
recommended practice to get that data in MacroA to UserFormA?

Thanks,

J









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Excel Info To Visio Rob Excel Discussion (Misc queries) 0 January 10th 07 08:25 PM
Passing Procuedure to Userform Nigel Excel Programming 4 September 10th 05 05:22 PM
Passing variables between Sub and Userform jose luis Excel Programming 8 July 22nd 05 05:20 PM
UserForm not passing Boolean variable Chip Pearson Excel Programming 2 July 19th 03 06:06 PM
Passing a value to a variable from Userform Neal Steiner Excel Programming 1 July 18th 03 09:12 PM


All times are GMT +1. The time now is 01:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"