Targeting Multiple Instances of Same Userform

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
559
I have used some sample code from Mike Rickson where you can add additional instances of the same userform dynamically as shown in this post:


The approach is to do the following in summary:
Code:
    Rem show new instace of Me
        Set newInstanceOfMe = UserForms.Add(Me.Name)
        newInstanceOfMe.Show vbModeless
        
    Rem unload that UF
        'Unload newInstanceOfMe
        Set newInstanceOfMe = Nothing

I am using this code to create multiple userforms from right clicking on items in my dashboard i made. The issue is I have tons of public subroutines that modify the userform interface and are found in modules outside of the userform module itself. So this is an issue as the scope of these subroutines if the new instance button is pressed it targets the original form as the form name is hard coded into the subroutines. I want to dynamically add the object to target the correct form (the one with the button being pressed). I could go back and put all of these functions in the userform and set to private but that would take quite a while. Is there a way to target this new userform because it has the same name as the original so I cannot do Userform("Name").label.caption="blah BLah BLah" . Any advice would be appreciated.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Amend the code to take an object argument (the form in question) and then pass the form to the routine using the Me keyword, or the relevant object variable (eg newInstanceOfMe)
 

Watch MrExcel Video

Forum statistics

Threads
1,130,122
Messages
5,640,243
Members
417,131
Latest member
Seanr19871

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
Top