VBA - Rename Form?

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
I have 2 forms that use all the exact same code behind them...just one is in color and one isn't....i want to give the user the option which one to choose...note: i created these in seperate worksheets and now want to combine them into one...both forms were named MAIN....and all the code references this form name...

my goal...rename both forms to something like MAIN_COLOR and MAIN_NOTCOLOR...then on start up rename or copy the one they want to use to just MAIN....is anything like this possible? if not any thoughts on how to proceed?

thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
I supoose you could try the following:

1) Give your user the option of either form & trap the response
2) Based on the form called, rename the active form to "Main"

I.E.

Code:
If Selection.Value = 1 Then
  FORM_MAIN.Name = "MAIN"
If Selection.Value = 2 Then
  FORM_COLOR.Name = "MAIN"
End If

And set the Form's name back to its original in its Unload event.

HTH,

Smitty
 

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
Smitty....thanks for the response...BUT that's what i orignally tried and got a compile error:

Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.

It has the .Name highlighted....

but that is essentially what i'm trying to do...any other ideas?

could i do something like set a public variable to my object...
not sure of exactly how to do that but i'm thinking maybe something like:

assume i have forms MAIN_COLOR and MAIN_NOTCOLOR

give user option to select which form...

set public variable (or something similar) to the userform selected (let's say we create variable vForm)

then reference in the modules via something like this

for each ctl in vForm controls

or

vForm.TextBox1.Text = "Test"

just trying to think out of the box...any help would be great....
 

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
I think I found out how to do it....

Code:
  Application.VBE.ActiveVBProject.VBComponents.Item("Main_COLOR").Name = "Main"
 
L

Legacy 98055

Guest

ADVERTISEMENT

You might run into problems using that code. Security settings may need to be altered by the user before your code will work. Why not use a single form and just allow the user to change the color scheme? Would that be too complicated? I do not know how your form is set up so I don't understand why you are going about this way. (creating two identical forms). In any case, the name property of a VB component is technically read-only. That is why you could not change the name.
 

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
thanks right_click...yeah i've been having issues on and off trying to use it...

i'll just go back to using two seperate files...but that just means i'll have to maintain code in 2 locations....

i'm developing an app but using two different front ends....not sure which one i like so i'm still doing both....once i make my mind up i'll just ditch the other one...oh well...i figured i'd give it a go...

thanks again
 
L

Legacy 98055

Guest
Well.. No reason to ditch your logic. If the code is identical in both, and all you need is to allow the user to select one of two forms, you could approach this in several ways. Don't use the name throughout your code but use a reference instead. Basically, this what you suggested in your second posting. See the example below...

TheNoocH.zip
 

TheNoocH

Well-known Member
Joined
Oct 6, 2004
Messages
3,482
Well.. No reason to ditch your logic. If the code is identical in both, and all you need is to allow the user to select one of two forms, you could approach this in several ways. Don't use the name throughout your code but use a reference instead. Basically, this what you suggested in your second posting. See the example below...

TheNoocH.zip

right_click....great stuff...that was exactly what i was aiming at with my second post...got it working now...took a little while to find out how and where i had to unload the form before reopening up the other one in my current code where i put this in....thanks alot for following through with a response....
 

Forum statistics

Threads
1,136,266
Messages
5,674,727
Members
419,523
Latest member
Urnovio

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