Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly rename CodeModule of ThisWorksheet

Tags:

People also ask

Can you rename VBA modules?

If you click on the module in the Project Explorer, the name should appear in the properties window (below). It should say "(name) Module1". You can just click on the name there and edit it.

How do I change the VBA worksheet name?

The user can easily change the name of a worksheet. Double-click on the worksheet tab and enter a new name, or right-click on an existing worksheet name, then choose Rename from the context menu. You can use VBA to change the name of a worksheet by assigning a new string value to the Name property.


I'm trying to rename the ThisWorkbook code module of an Excel worksheet using VBA Extensibility.

I accomplish this with the line

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").Name = "wb"

I can see the change in the VB editor and also access the workbook's properties, e.g. Debug.? wb.Name.

However: If I save and close the file and then reopen it, I get strange behavior. If the code module was empty before renaming it, it reverts back to the old empty ThisWorkbook name.

If it was not empty or was populate before the saving, I now have both, an empty ThisWorkbook module (that would fire events if there were any) - and the filled wb module - which does not fire workbook events:

enter image description here

Has anyone seen this behavior - and knows a fix/workaround?