I was working on adjusting some variable naming on an Excel project and ran into an issue that the MSForm control names needed to be updated. When you change the properties of the control on the form, the underlying code for the form doesn't seem to update accordingly.Examples:You rename btn_button1 to btn_populateList. Now when you click the button it still wants to call btn_button1_Click() rather than  btn_populateList_Click().You rename inputbox1 to listToPopulate but when you run pre-existing code, it still refers to inputbox1 instead of the new  name of the control.Anyhow, I've always wanted to mess around to get VBA to do stuff in the VBE, so this was my chance!The macro finds the target project and target form and then updates the control names and appends the string NEW to oldName such that it's oldNameNEW. This was done so I could easily revert the names back using the same procedure with left(len()-3). Ideally, the array of new names would be populated by the user with descriptive names and would be populated outside of the For loop, obviously.I'm sure there's some clever refactoring that can be done, but I wasn't sure if I'd break it.Also, I'm worried I misunderstand how Forms call their macros given it's not designed the same way as controls on sheets.Public Sub FindReplaceInEntireModule()    Const TARGET_ADDIN As String = bUTLAddIn    Const TARGET_FORM As String = form_chtGrid    Dim targetProject As VBIDE.VBProject    Dim targetModule As VBIDE.VBComponent    Dim targetCode As VBIDE.CodeModule    Dim controlIndex As Long    Set targetProject = Application.VBE.VBProjects(TARGET_ADDIN)    Set targetModule = targetProject.VBComponents(TARGET_FORM)    Dim targetForm As Object    Set targetForm = targetModule.Designer    Dim numberOfControls As Long    numberOfControls = targetForm.Controls.count    Dim newControlNames() As String    ReDim newControlNames(1 To numberOfControls)    Dim oldControlNames() As String    ReDim oldControlNames(1 To numberOfControls)    For controlIndex = 1 To numberOfControls        oldControlNames(controlIndex) = targetForm.Controls(controlIndex).name        newControlNames(controlIndex) = oldControlNames(controlIndex) & NEW        targetForm.Controls(controlIndex).name = newControlNames(controlIndex)    Next    Set targetCode = targetModule.CodeModule    Dim findWhat As String    Dim replaceWith As String    Dim numberOfLines As Long    numberOfLines = targetCode.CountOfLines    Dim lineNumber As Long    Dim lineText As String    For controlIndex = 1 To numberOfControls        findWhat = oldControlNames(controlIndex)        replaceWith = newControlNames(controlIndex)        For lineNumber = 1 To numberOfLines            lineText = targetCode.Lines(lineNumber, 1)            If InStr(1, lineText, findWhat, vbTextCompare) > 0 Then                targetCode.ReplaceLine lineNumber, Replace(lineText, findWhat, replaceWith, , , vbTextCompare)            End If        Next    NextEnd Sub
Renaming form controls and underlying code
vba;excel
You've basically implemented (parts of) a rename refactoring.The problem is that the VBIDE API only gives you strings to work with, so you essentially have a smart find & replace, but it's still a find & replace.A LOT of things can go wrong when processing code without a symbol table.Imagine a control named MyButton that you want to rename to SomeButton. Then you have this code in the module:Dim MyButtonForeColor As LongMyButtonForeColor = vbBlackYour code will rename MyButtonForeColor too, because it contains MyButton - but did you mean to? In this case, probably.UserForm controls are Public, so you can have code outside the form that does this:With New UserForm1    .Show vbModal    ActiveSheet.Cells(1, 1) = .SomeTextBox.TextEnd WithAnd that code will break when you rename SomeTextBox to anything else.Or, you could have conflicting but unambiguous names in different scopes, that a simple find & replace will break:UserForm1.SomeButton.Caption = UserForm2.SomeButton.CaptionNaming is hard. Renaming is even harder.Having worked on Rubberduck for over two years, I can assure you that there is absolutely no way to implement any kind of rename refactoring without a symbol table - and that means lexing, parsing and resolving the VBA code you want to refactor.Ctrl+H isn't a refactoring tool. Rubberduck is. I don't mean to sound like an ad, but your code needs to know exactly which code across the entire project is referencing the control you're renaming - and that simply isn't possible to implement reliably in VBA. Heck, Rubberduck has a proper parser and resolver, and still struggles with a number of edge cases.I give you this code to play with - it's EVIL beyond words, I know... but it's 100% legal VBA code, and Rubberduck sorts it all out:'Project Name = MyProject'Module Name = MyModuleOption ExplicitPrivate MyVar As MyProjectPrivate MyVar1 As MyModulePrivate Type MyProject  MyModule As String  MySub As StringEnd TypePrivate Type MyModule  MyVar As String  String As MyProjectEnd TypePrivate Type MySub  MyVar As MyModule  MyVar1 As MyProjectEnd TypePrivate Type MyVar  MyProject As MyModuleEnd TypeSub MySub()  Dim MyProject As MyProject  Dim MySub As MySub  Dim MyVar As MyVar  MyVar.MyProject.MyVar = Smith  MySub.MyVar1.MySub = MyProject.MySub  'My brain hurts....End SubYour strategy (I know you're only looking at controls, but the principle is the same) will fail to correctly rename MySub here (whichever you pick).Your code looks great though - good naming, clean code, nice spacing, indentation, casing, a bit large for a single procedure though, but as I said I don't think any of it is relevant, since there's no way any identifier-renaming VBA macro can work [in all cases] simply by looking at context-less strings.