Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 15
Default Running UserForm1 routines from UserForm 2

Hi,
I have 2 userforms. Userform1 works great and now I'm creating
userform2. In userform 2 I'd like to call a couple of the routines
from Userform1. Is this possible? and if yes, how should I
approach it.
Thanks
Chuck
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default Running UserForm1 routines from UserForm 2

By default, userform subroutines are private, which I think means
that they cannot be called from outside that userform module.
However, you can rename the Userform subroutines from
"Private" to "Public". (Not sure if this is a good idea or not)

Once the userform subroutine is "Public", you can simply call
any userform module as follows from anywhe

Call UserForm1.UserForm_Activate
Call UserForm1.UserForm_Initialize

etc.... etc....


"chuckm" wrote in message
...
Hi,
I have 2 userforms. Userform1 works great and now I'm creating
userform2. In userform 2 I'd like to call a couple of the routines
from Userform1. Is this possible? and if yes, how should I
approach it.
Thanks
Chuck


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 661
Default Running UserForm1 routines from UserForm 2

Save the code in a normal code module, then call it from each of the user
forms.
--
If the post is helpful, please consider donating something to an animal
charity on my behalf .......... and click Yes


"chuckm" wrote:

Hi,
I have 2 userforms. Userform1 works great and now I'm creating
userform2. In userform 2 I'd like to call a couple of the routines
from Userform1. Is this possible? and if yes, how should I
approach it.
Thanks
Chuck
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 789
Default Running UserForm1 routines from UserForm 2

Hi
In a normal code module have

Public mysub(parameterlist)
'stuff
End Sub

For each Userform simply do

Sub userform1_initialize() 'or whatever
Call mysub parameterlist
End sub

regards
Paul

Userform1 might
On Feb 12, 8:41*pm, chuckm wrote:
Hi,
I have 2 userforms. * *Userform1 works great and now I'm creating
userform2. * In userform 2 I'd like to call a couple of the routines
from Userform1. * * * Is this possible? *and if yes, how should I
approach it.
Thanks
Chuck


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Running UserForm1 routines from UserForm 2

To do it right, you should have a reference in UserForm2 that points
back to UserForm1. For example, in UserForm1, place a command button
on the form to call up UserForm2:

' in UserForm1
Private Sub btnShowForm2_Click()
Load UserForm2
Set UserForm2.Form1Ref = Me
UserForm2.Show
End Sub

Then, in the UserForm1's code module, mark as Public the method(s) in
UserForm1 that you want to call from UserForm2. E.g.,

' in UserForm1
Public Function SquareRoot(D As Double) As Double
SquareRoot = D ^ (1 / 2)
End Function

Then, in UserForm2, create a Public variable to hold the reference to
UserForm1:

' In UserForm2
Public Form1Ref As UserForm1

Now, you can call the method in UserForm1 from code in UserForm2. The
following command button code calls SquareRoot in UserForm1.

' In UserForm2
Private Sub btnCallBackTo1_Click()
Dim D As Double
Dim E As Double
D = 9
E = Form1Ref.SquareRoot(D)
MsgBox E
End Sub

The actual linkage between the forms objects is in the line

Set UserForm2.Form1Ref = Me

Here, Form1Ref is defined as type UserForm1 and Me refers to the
instance of the object that contains the keyword Me, which is, of
course, Userform1.

All of this said, I would agree with the other replies that it might
be better to take the code out of the userform's modules and put it in
a standard module. However, if the code in UserForm1 that is called
from UserForm2 needs data that resides on UserForm1, then the
mechanism outline above would be the better approach.



Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]










On Fri, 12 Feb 2010 12:41:42 -0800 (PST), chuckm
wrote:

Hi,
I have 2 userforms. Userform1 works great and now I'm creating
userform2. In userform 2 I'd like to call a couple of the routines
from Userform1. Is this possible? and if yes, how should I
approach it.
Thanks
Chuck

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
running program from userform davegb Excel Programming 10 November 2nd 06 04:57 PM
Running GIF in userform Graham Whitehead Excel Programming 0 August 7th 06 05:11 PM
What's the difference between 'Set UserForm1=Nothing' and 'Unload UserForm1' ? Zoo Excel Programming 1 May 11th 06 04:18 PM
SetFocus to first textbox on userform upon Userform1.Show Paul Simon[_3_] Excel Programming 6 February 11th 04 04:31 PM
How can I use a worksheet while running a userform? Leo Excel Programming 4 December 17th 03 02:58 PM


All times are GMT +1. The time now is 06:41 PM.

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

About Us

"It's about Microsoft Excel"