Renaming UserForm Controls

Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator
2016-12-30T01:04:10+00:00

I have a large project that will have many (~15) userform Frames, each of which will contain 600 controls (150 labels that are captioned at run time and 450 option buttons (150 groups of 3).

Having gone through the tedious task of setting up the first frame and assigning unique names to each of the 600 controls, and copying that frame to another userform for the purpose of being able to work on it, I was looking for a way to use VBA to rename each of the controls for which I came up with the following code

Sub rename()

With Application.VBE.ActiveVBProject.VBComponents.Item("frmWorkSpace").Designer.Controls("S05")

   For i = 1 To .Controls.Count

        If Left(.Controls(i).Name, 4) = "QS04" Then

            .Controls(i).Name = "QS05" & Mid(.Controls(i).Name, 5)

        End If

    Next i

End With

End Sub

While the code does exactly what I need it to do, in that it successfully renames each of the controls, when it is run, it results in 

and when I click on Debug,  I see:

Does anyone have an idea why that error is occuring, or another way of re-naming controls using VBA?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2016-12-30T20:39:35+00:00

    Hi Doug,

    To assist you further with Microsoft Office concern, it would be best to post your query here.

    Regards.

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator
    2016-12-30T22:22:18+00:00

    So just what is the purpose of the Microsoft Office Programming Forum if it is not to ask questions about programming the Office applications?

    The issue that I raised had nothing at all to do with Planning, Deployment and Compatibility.

    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 322.1K Reputation points MVP Volunteer Moderator
    2017-01-01T07:50:21+00:00

    The issue with my code was that the array of controls is indexed from 0.  Therefore, I should have been using:

    For i = 0 To .Controls.Count - 1

    1 person found this answer helpful.
    0 comments No comments