Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code inspection for square-bracketed names in Excel VBA #6210

Open
joespiff opened this issue Apr 16, 2024 · 1 comment
Open

code inspection for square-bracketed names in Excel VBA #6210

joespiff opened this issue Apr 16, 2024 · 1 comment
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope.

Comments

@joespiff
Copy link

Justification
In Excel VBA, a named range can be used in code by typing the name in square brackets. This causes the code inspection message "Expression '[rangename]' cannot be validated at compile-time". The description for this inspection suggests to "Consider using the host application's object model instead." However, these square-bracketed ranges are a very useful way to validate which named ranges are used in code, so it can be a good practice to use them, as long as they are converted to a strongly-typed variable as soon as possible.

Description
If the developer only uses the square-bracketed expression to set a strongly-typed variable, then this message should not display. For other uses of square-bracketed names in Excel, a refactor should be offered that will declare a range and set it equal to the square-bracketed name.

@joespiff joespiff added the enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. label Apr 16, 2024
@retailcoder
Copy link
Member

Thanks for the feedback!

If I understand correctly, the idea would be to avoid issuing a result for this inspection when the bracketed expression is found in the RHS of a SetStmt that has an Excel.Range variable its LHS, only when the host application is EXCEL.EXE:

Dim Cell As Range
Set Cell = [expression]

This seems sensible, however I would point out that the bracketed expression is, as pointed out by the inspection, inherently late-bound: there is no way to correctly evaluate what's in it without having Excel perform its evaluation - so it's a little bit "wishful thinking" to assume it's only ever contain range names or addresses (they can be literally anything Excel is capable of evaluating), and validating this would be creeping into attempting to evaluate late-bound expressions, which Rubberduck should not be trying to do.

If I may suggest an alternative, I would submit that embracing early binding and using Workbook.Names and Worksheet.Names collections to retrieve scoped Name objects (Name.RefersToRange resolves the Range for it) makes it even easier to track what names are referenced in code; because Rubberduck knows about every single place a Name object is being used, listing all references to Names collections would list all the places these collections are being referenced: "Find all references" works for anything that has a name, regardless of whether it's VBA project code or compiled referenced library code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope.
Projects
None yet
Development

No branches or pull requests

2 participants