Polo
Polo

Reputation: 1

Change of a textbox.name in a form by code

I have a Form in an Excel VBA project that has many textboxes with names I'd need to change. For instance, names in the projects are: Fees01TBX; Fees02TBX; Fees03TBX, and so on. There are 40 textboxes like these, and I need to change the names into a sequence I can manage better. For instance, I'd need to have them with these names: Expenses01; Expenses02, Expenses03, and so on.

I tried to change it using some code like (just for one textbox):

Sub ChgName()    

    MyForm.Fees01TBX.Name = "Expenses01"
    'Doing this for each one would not be a problem, I can array the sequence.
End Sub

I think it should possible to change the name of a textbox in a form by code, but how?

Upvotes: 0

Views: 1838

Answers (1)

EvR
EvR

Reputation: 3498

Make sure you ticked Trust Access to the VBA Project Object Model in security settings:

Sub Rename()
    With ThisWorkbook.VBProject.VBComponents("MyForm")
        .Designer.Controls("Fees01TBX").Name = "Expenses01"
    End With
End Sub

But if Fees01TBX had code you have to change this as well, but for 40 TextBoxes you could do it manually

Upvotes: 1

Related Questions