Trying to Replace X for Y across all UserForm Control Names

Sal Paradise

Well-known Member
Joined
Oct 23, 2006
Messages
2,457
I have two identical userforms. I want to give them different names. If the word Google appears in the UserForm, I want to swap it with Yahoo.

I figured this would work:
Code:
Sub Test()
    
    Dim Ctl As Control
    
    Application.EnableEvents = False
    
    For Each Ctl In Google.Controls
        Ctl.Name = Replace(Ctl.Name, "Google", "Yahoo")
    Next Ctl

    Application.EnableEvents = True

End Sub
Unfortunately, it keeps giving me an error:
Run-time error '382':

Could not set the Name property. Can not set property at run-time.
How the dickens do I get it to loop through my controls and change names? Even if I try to change a single name by code manually (Google.SearchName.Name = "YahooSearch") it doesn't do it, giving the same error.

Any ideas?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Sal

Try:

Code:
Sub Test()
    
    Dim Ctl As MSForms.Control
    Dim Ctls As MSForms.Controls
    
    Application.EnableEvents = False
    
    Set Ctls = Workbooks("Book3").VBProject.VBComponents("Google").Designer.Controls
    
    For Each Ctl In Ctls
        Ctl.Name = Replace(Ctl.Name, "Google", "Yahoo")
    Next Ctl
    Application.EnableEvents = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,132,668
Messages
5,654,644
Members
418,146
Latest member
Shnn028

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