I am looking for alternative, general coding methods for dealing with global named ranges, in VBA. I'm hoping for answers, here, with some new generalized suggestions and approaches.
I suggest a few methods I've used, but the methods don't avoid all problems -- I would like: ease of coding and spreadsheet drafting; tolerant of spreadsheet changes, and; ease of lookup/reference months later.
As I draft a spreadsheet that will later use VBA, I create named ranges (typically global names) in spreadsheet formulas. The ranges are useful there on the sheets, and useful as a reference from VBA. Typically, I do not add/change the names collection in VBA; I merely reference the collection.
When coding VBA, I access named ranges created in the workbook. If I cut/paste named cells/ranges, editing the workbook and sheets, the VBA still works.
Yet, Global Names, created in a worksheet environment, don't meet all three requirements in the VBA environment -- especially when modifying code or modifying the worksheets.
In My Perfect World:
wb.range("myGlobalRangeName")
My Perfect World hopes that a workbook's global references created on a spreadsheet are global -- but without clarification VBA expects that reference to be on the ActiveWorkBook and ActiveWorkSheet.
Thought One: I know that Range("myGlobalRangeName")
accesses that range, and
Thus, I often use this fragment
wb.Worksheets("SheetOne").Range("myGlobalRangeName")
maybe constructed inside With
Blocks, specifying the worksheet even though the range is a global reference.
Moving named cells to other sheets breaks this reference (even though the Name is Global!). I have to backtrack through all the code looking for misplaced references; or I can execute the code and hope to catch the errors...
Thought Two: I can write, instead, something like this, to access the name collection for the workbook:
wb.Names("myGlobalRangeName").RefersToRange
but having to append RefersToRange
is .... well ... annoying. It misses the simplicity of the perfect world.
Thought Three: I create a distinct worksheet
with all the values I want to trap in the other sheets, and I create ranges on that distinct sheet, only. Cell references in the workbook and accessed with VBA both work. That way, the VBA begins with
Dim wsNames as spreadsheet
set wsNames = wb.worksheets("SheetWithNames")
and the name references always used wb.wsNames
, and looked like this:
with wb
.... .wsNames.range("myGlobalRangeName") ....
end with
or other useful variations.
Yet this, too, can get messy -- I have to backtrack to see where the real data is when I later amend the spreadsheet or the VBA. Sometimes, that method works, particularly if I tenaciously name ranges on that sheet only for VBA consumption, use really memorable names, and remember the locations, and remember I did all this months later...
Conclusion: Am I missing something? Are there other, maybe easier, general coding methods other than using
RefersToRange
with thename collection
, orwb.worksheets("SheetName").Range("myGlobalRange")
references which explicitly identify the [current] sheet, or- placing RangeNames and referral formulas on a separate sheet, with VBA range references as
wsNames.Range("myGlobalRange")
.
I don't want to type so much; or create procedure variables to trap values before using them in assignments. The mess gets...well...harder to read and worse to track if I am assigning one cell's value to another in another workbook, and one or both use global ranges.