Rubberduck
Rubberduck copied to clipboard
Replace CodeName with Me keyword in code behind for same worksheet
What
When a sheets name is used in its own code behind pane replace it with the Me
keyword.
Why
Use of the Me
keyword makes it explicit the intention is to work with the sheet the code is within.
Example This code should trigger the inspection:
Public Sub DoSomething()
Sheet1.Range("A1").Value2 = "Populated value"
End Sub
QuickFixes
Should Rubberduck offer one or more quickfix(es) for this inspection? Describe them here (note: all inspections allow for IgnoreOnceQuickFix
, unless explicitly specified):
-
ReplaceCodeNameWithMeKeywordQuickFix
Example code, after quickfix is applied:
Public Sub DoSomething() Me.Range("A1").Value2 = "Populated value" End Sub
Resources Each inspection needs a number of resource strings - please provide a suggestion here:
- ReplaceCodeNameWithMeKeywordInspection: the name of the inspection, as it appears in the inspection settings.
-
InspectionInfo: Renaming a worksheet's CodeName without Rubberduck can break the intention of this statement execution. Use the
Me
keyword to make the intended sheet usage explicit. -
InspectionResults:
The {0} codename can be replaced with the Me keyword.
, {0}: codename
This is a good inspection idea imo.
I just had the thought, should it generalize to any host application document object? If I understood it correctly at some level VBA doesn't distinguish between an Excel spreadsheet and whatever else any other host application wants VBA to treat as special citizens that can host code behind?
AFAIK, Excel is the only object model that has a thing like CodeName
where you can change the name of a document module.
In other object models (e.g. Access), the only way to rename is via its object model, not via VBA/VBIDE. Other hosts may provide their own approach (or not at all) so it's not generalizable.
Does the document object interface just have some attribute to indicate to the VBE whether or not it is allowed to rename them?
No special attribute that I am aware of. The real problem is that you have to go through the host's object model and Rubberduck is a VBIDE add-in and should be host-agnostic. Going through the host's object model is problematic.
From reading the RD blog post on this I thought the hosts object model was just on an interface with VBA/VBE itself:
They’re in the VBA project, because the host application decided that’s what’s in a VBA project hosted in – here Excel, but each host application gets to decide whether a project includes a module to represent the host document, and under what circumstances to add or remove other types of modules, like Worksheet, or Chart.
I definitely don't understand this part of VBA very well. Hard to find resources other than RD blog that explain it.
Okay thinking about this more, wouldn't there be at least a bit of generalization, like wouldn't you want the inspection to examine userforms too, for example?
linking #2188