Whithin the same document (workbook), whenever I create named ranges, I have something like Sheet1!A1 but it is always refering to Sheet1. Is there any way in which I could refer to the current spreadsheet instead?
Something like this!A1 which will turn into Sheet1!A1 if I’m currently in Sheet1 or Sheet200!A1 if I’m in Sheet200.
The purpuse is to reuse the same formulas (with already have the named intervals) in all the spreadsheets of the workbook. I found it rather stupid this global and fixed reference. This way I always have to recreate my formulas for each spreadsheet.
A named range refers to a specific range of cells on a specific sheet – I don’t see that changing anytime soon.
But here is a custom function you can put into your spreadsheet’s script that will “localize” ranges for you. This code is also available for download in this Gist.
Here’s an example. NamedRange1 has been defined as “Sheet1!A10:A22”. If we put
=localizeNamedRange(NamedRange1)in a cell, it will returnA10:A22, without the reference toSheet1. That’s a string, so if we want to use it in a formula that expects a range, we need to convert it – and do that, we use the built-in functionINDIRECT().On Sheet1 we have a formula in cell A3 that calculates the average value of NamedRange1. Here’s how to define that formula to make it localize-able:
If you now copy Sheet1 into a new Sheet2, you’ll find that the formula still works, and that it performs its calculations on the data on Sheet2, even though the Named Range refers to Sheet1.