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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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