patternMinor
Function to return a legal name for an Excel range: Follow-up
Viewed 0 times
excelreturnrangefunctionlegalnameforfollow
Problem
This Excel VBA function was originally the subject of a previous question on Code Review. This function (
In the previous question AlexR raised the point that a
What I'm asking now is: can you break its functionality?
TLDR?
You can download an .xlsm file here for testing the function, or to see all the code in one piece if you don't want read this lengthy post in its entirety.
Intended scope of
My vision is for
Overview of revisions to
Namify()) takes a string as input, modifies it to make it valid for use as a range name, and then returns the valid string. I've revised the function based on some excellent input from Mat's Mug and RubberDuck and I feel it's about as rigorous as I can make it. In the previous question AlexR raised the point that a
VBScript.RegExp could greatly streamline the function, however I'm using Excel for Mac 2011 which doesn't natively support VB regular expressions. You should check out his post if you'd like to clean up the function for yourself.What I'm asking now is: can you break its functionality?
TLDR?
You can download an .xlsm file here for testing the function, or to see all the code in one piece if you don't want read this lengthy post in its entirety.
Intended scope of
Namify():My vision is for
Namify() to serve as a tool that can be drawn upon by a subroutine handling the naming of Excel objects. The Sub would source user input (or some other source) to get a desired or initial name, and then pass that name to Namify() to make it legal and optionally verify that it isn't already in use or increment it to make it unique if it is in use. The way I see it, how the name is applied to objects (overwrite name/reference, add name, etc...) is beyond the scope of Namify(), but rather is within the scope of the the Sub that calls Namify(). Overview of revisions to
Namify():- Converts (some) unicode accented characters to standard equivalent.
- Validation of characters now done by
InStr()instead ofMatch().
- No longer removes periods and backslashes from names.
- Invalid characters removed from name instead of being replaced with underscores.
- Throws the user a message if the modified string is longer than 255 characters and exits.
- Prefixes with underscore if string begins with a number OR a period.
- Two optional argument
Solution
Standard response - use
Overall I can't say much about the method, but I can talk about variables
I'm not entirely sold on your naming of variables:
I'm also no terribly sold on using
And if I understand correctly
You have a lot of comments explaining what is happening, but the code should speak for itself. If you need to explain it, explain why you're doing it this way rather than that way:
By the way, your
How can you overwrite a name?
Defining a range
Option Explicit to catch any variable issues of which there are none! Good job!Overall I can't say much about the method, but I can talk about variables
I'm not entirely sold on your naming of variables:
- validchars -
validChars
- i - I'll give you
i, But I won't give youaorB- why is one lower and one upper?
- invalidfirsts -
Const invalidFirsts as String = ".234567890"
- invalidsingles -
Const invalidSingles as String = "CcRr"
- isused - Is used? maybe
inUse
- wbnames -
WorkbookName, or better yet, give the workbook a name in VBA to do away with this variables.
- nameindex -
nameIndex
- incrementused - incrementUsed
AccCharsandRegCharsare nitpicky - the first letter should be lowercase
I'm also no terribly sold on using
Verify as a variable - maybe Option ByVal verifyName as Boolean. Using Verify might confused the reader as being an excel method or function.And if I understand correctly
MakeUnique will make name if used, into name1? That's not a great practice, you'll easily get confused by name1, name2, name3 - you'll constantly be opening the Name Manager. Perhaps prompt for a new name?You have a lot of comments explaining what is happening, but the code should speak for itself. If you need to explain it, explain why you're doing it this way rather than that way:
'Make sure the name is <= 255 characters could be 'Excel doesn't allow names longer than 255 charactersBy the way, your
namify handles formulas pretty well.How can you overwrite a name?
Defining a range
TRUE twice throws error 450.Context
StackExchange Code Review Q#119307, answer score: 3
Revisions (0)
No revisions yet.