简体   繁体   English

如何引用具有偏移量的命名范围中的单元格?

[英]How can I reference a cell in a named range with an offset?

I'm trying to work out how in VBA to read and update a particular row in one range, based upon the row I'm looking at in another range the same size.我正在尝试根据我在另一个相同大小范围内查看的行,在 VBA 中如何读取和更新一个范围内的特定行。

So let's say A3:A9 is one range "Alpha", and F18:F24 is the other range "Foxtrot".因此,假设 A3:A9 是一个范围“Alpha”,而 F18:F24 是另一个范围“Foxtrot”。

I know that A5 has been clicked and since querying the Row property of the "Alpha" returns the starting row for the range, I can determine the location of the cell clicked within Alpha with the code:我知道 A5 已被单击,并且由于查询“Alpha”的 Row 属性返回范围的起始行,我可以使用代码确定在 Alpha 中单击的单元格的位置:

iRowOffset = cell.Row - Range("Alpha").Row

I've figured out how to read the same row in Foxtrot我已经想出了如何在 Foxtrot 中读取同一行

Range("Foxtrot").Value2(iRowOffset + 1, 1)

What I can't figure out is how to actually update Foxtrot我不知道如何实际更新 Foxtrot

Range("Foxtrot").Value2(iRowOffset + 1, 1) = 3

doesn't error, but also doesn't update the value in the spreadsheet, or in the Range object when viewed through Locals.不会出错,但也不会更新电子表格中的值,或者通过 Locals 查看时范围 object 中的值。

What I'm trying to avoid is hardcoding the relative positions of the two ranges, eg by offsetting from the cell in Range("Alpha") since I'd like the flexibility to move Foxtrot around on the sheet without having to worry about updating the code.我要避免的是硬编码两个范围的相对位置,例如通过从 Range("Alpha") 中的单元格偏移,因为我希望灵活地在工作表上移动 Foxtrot 而不必担心更新编码。

Range("Foxtrot").Value2(iRowOffset + 1, 1)

indexes into the array returned by .Value2 . .Value2返回的数组的索引。

You need to actually refer to a Range object.您实际上需要参考Range object。

Range("Foxtrot").Cells(iRowOffset + 1, 1).Value2 = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM