Two userforms - calling a userform method from another userform

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
So I have two userforms. I kind of get how to call a subroutine of a userform from another:

1. Ensure you declare the subroutine as Public
2. From Userform2, call the subroutine in Userform1 by using: Call Userform1.NameofSubRoutine

I'm following the progression of the code by stepping through the code. I'm finding that the code doesn't immediately run through Userform1.NameofSubRoutine, but instead first runs through Userform_Initialize of Userform1 before going to NameofSubRoutine of Userform1.

Why question is why does this happen? And is there anything I can do to prevent this from happening?
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I'm guessing that Userform1 is not open when this happens.
If you Load Userform1 before calling Userform1.NameOfSubRoutine, it should run without the intialize event.

Why is that if you refer to an un-loaded userform's properties or methods before it is loaded, then Excel automaically creates an instance of that userform

Typically one would use that feature to pre-load the form with values before showing it, like this (in a normal module)

Userform1.TextBox1.Text = "Hello"
Userform1.Show

Insert some testing line and see what happens when

VBA Code:
MsgBox Userforms.Count: rem returns 0

UserForm1.TextBox1.Text = "Hello"

MsgBox Userforms.Count: rem returns 1

Userform1.Show

MsgBox Userforms.Count: rem returns 0
 
Upvote 0
But the odd thing is, both userforms are open. So I'm not sure why calling a subroutine in the first userform from the second userform would trigger a re-running of the initialization subroutine of the first.
 
Upvote 0
That is odd.
Which userform is opened first?

One weird thought that I have is that the Call Userform1.NameofSubRoutine line might be creating a new instance of Userform1.

If you add, this to the Initialize event, that would tell us what is going on.
VBA Code:
MsgBox Userforms.Count & ":" & Me.Tag
Me.Tag = "test"

If that is the problem, the solution would be to make sure that the UF2 code is addressing the existing instance of Userform1.

VBA Code:
Dim oneForm as Object, MyUF1 as Object

For Each oneForm in Userforms
    If oneForm.Name = "UserForm1" Then
        Set MyUF1 = oneForm
    End If
Next

Call myUF1.NameOfRoutine
 
Upvote 0
That first code - you mean to add it to the Initialize event of Userform #1?

To answer your question - userform #1 was opened first, then I open userform #2 from a button on userform #1. I make some selections on userform #2 and click a button on userform #2 to run a subroutine belonging to userform #1 (I love how 'userform' has to be added to the spell-check dictionary) :)
 
Upvote 0
The code that launches userform2 is:

VBA Code:
Private Sub UserForm2Launch()

Dim frm As CategoryChoice
Set frm = CategoryChoice
frm.Show vbModeless

End Sub
 
Upvote 0
Okay, did that and no change. It still calls the Userform1 Initialize subroutine prior to the UF1.NameOfSubRoutine
 
Upvote 0
So Mike, I fixed the problem. The changed the way the FIRST userform was called:

Old Way :
VBA Code:
Dim frm As SummaryGraph
Set frm = New SummaryGraph
frm.Show vbModeless

New Way:
VBA Code:
Dim frm As SummaryGraph
Set frm = SummaryGraph
frm.Show vbModeless

Summary Graph is Userform1. So when I call Userform1.NameofSubRoutine using the 'new way', only the subroutine is called not the initialize event of userform1. Any idea of what is going on here?
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,702
Members
449,464
Latest member
againofsoul

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