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

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
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,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

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