简体   繁体   English

如何防止命名范围公式使用VBA更改单元格值

[英]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.

相关问题 VBA如何将命名范围名称传递给公式而不是单元格内容 - VBA how to pass named range Name into a formula rather than a cell content 如何在VBA中保留单元格更改的记录 - How to keep record of cell changing in vba Excel VBA从工作表中的公式创建一个命名范围 - Excel VBA create a Named range from formula in worksheet VBA:如何索引公式中命名范围的每个值? - VBA: How to index through each value of a named range in a formula? 如何使用包含某些公式的Excel VBA定义命名范围 - How to define a named range using excel vba that includes some formula Excel VBA如何在R1C1公式中保留可变单元格 - Excel VBA how to keep a variable cell in a R1C1 formula 在 Excel/VBA 中,如何根据单元格值将公式应用于范围? - In Excel/VBA, how to apply a formula to a range based on cell value? Excel 2010 VBA Formula1引用带有选项卡名称变量的命名范围*或*单元格位置 - Excel 2010 VBA Formula1 Referencing a Named Range *or* cell location with variable for tab name VBA Excel。 根据与命名单元格的偏移量编辑范围 - VBA Excel. Edit a range based on an offset from a named cell 使用事件触发器将值从一个范围粘贴到一个单元格和另一个范围,并使用 sum 函数来保持运行总计 [VBA] - Use Event Trigger to paste values from one range to a cell and another range and use the sum function to keep a running total [VBA]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM