R1C1 的绝对引用使用变量来听写行号 (VBA)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27901637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Absolute reference with R1C1 using a variable to dictat row number (VBA)
提问by user4445078
I have a code that creates a number of separate lists under each other. For each of these lists I'm running a for loop to assign an equation to each row at the end column. This formula should multiply the neighbour cell (a relative reference) with cell at the top of that particular list (an absolute referance). The problem is that the lists are of arbitrary length and generated earlier in the code, so I can't assign the absolute referance beforhand.
我有一个代码,可以在彼此之下创建许多单独的列表。对于这些列表中的每一个,我都运行一个 for 循环来为末尾列的每一行分配一个方程。此公式应将相邻单元格(相对参考)与该特定列表顶部的单元格(绝对参考)相乘。问题是列表的长度是任意的,并且在代码中较早生成,因此我无法事先分配绝对引用。
I was thinking on saving the row number (row 1 = 1, row 2 = 2 etc) in a variable and then use the variable in the R1C1 notation (= "R(variable)C5*RC[-1]), but I cant seem to get this to work... The variable will be the same throughout the for loop given in the example below, but will change next time the same for loop is entered.
我正在考虑将行号(第 1 行 = 1,第 2 行 = 2 等)保存在一个变量中,然后在 R1C1 表示法中使用该变量(=“R(variable)C5*RC[-1]),但我似乎无法让它工作......在下面示例中给出的整个 for 循环中,变量将相同,但下次输入相同的 for 循环时将更改。
Is this even possible?
这甚至可能吗?
(I know the parantheses in the R1C1 are not the proper notation, but this is to show where I want my variable)
(我知道 R1C1 中的括号不是正确的符号,但这是为了显示我想要变量的位置)
...
variable = 3
For i = 1 to count
last = ActiveSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1
Cells(siste, "E").FormulaR1C1 = "=R(variable)C5*RC[-1]"
Next
采纳答案by Rory
Just one small change:
只是一个小改动:
Cells(siste, "E").FormulaR1C1 = "=R" & variable & "C5*RC[-1]"