[英]How to keep named range formula from changing cell values with VBA
I noticed an interesting problem while trying to debug a VBA routine that sorts the list of worksheets in a range and then redraws the border around that range. 我在尝试调试VBA例程时注意到一个有趣的问题,该例程对一个范围内的工作表列表进行排序,然后在该范围周围重新绘制边框。
The range containing is defined in Name Manager with the formula as shown below 包含的范围是在名称管理器中使用以下公式定义的
=Tables!$L$2:$L$22
The problem that is occurring is that while doing the sheet name work, sometimes cells are deleted and sometimes cells are inserted. 发生的问题是,在执行工作表名称工作时,有时会删除单元格,有时会插入单元格。 This CHANGES the cell address values in the named range.
这会在指定范围内更改单元格地址值。 So if I deleted two cells and inserted one cell, the formula changes to
因此,如果我删除了两个单元格并插入了一个单元格,则公式将变为
=Tables!$L$2:$L$21
And if I happen to insert into the first cell (L2), then the formula changes to 如果我碰巧要插入第一个单元格(L2),则公式更改为
=Tables!$L$3:$L$22
I'm certain that problem can be solved using the header range name and offsetting by one, but I'm not sure how to do that in the formula for a named range as I've tried numerous ways and can't get it right. 我确定可以使用标头范围名称并将其偏移一个来解决问题,但是我不确定如何在命名范围的公式中执行此操作,因为我已经尝试了多种方法并且无法正确处理。 But I also need the ending range address to be static.
但是我还需要结束范围地址为静态。
Any help appreciated. 任何帮助表示赞赏。
One thing to try is to use: 要尝试的一件事是使用:
=Indirect("Tables!$L$2:$L$22")
rather than 而不是
=Tables!$L$2:$L$22
It is an acceptable Name, but I am not sure you will get the same functionality. 这是可以接受的名称,但是我不确定您是否会获得相同的功能。
Found a better way than the comment using the static header range. 找到了比使用静态标头范围的注释更好的方法。 You simply define the whole range using the offset function from the header.
您只需使用标头中的offset函数定义整个范围。
=OFFSET(AllSheetsHeader,1,0,21,1)
Where the 1,0,21,1 are 1 row offset from header, 0 column offset, 21 is row height, and 1 is just that one column 其中1,0,21,1是距标头的行偏移量,0列偏移量,21是行高,而1只是那一列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.