我希望能够根据行/列变量在 Excel 中动态指定范围。
假设我有一个文件,其内容看起来有点像这样:
A B C D E 1 10 11 12 13 14 2 51 52 53 54 55
如果我想对第 1 行第 2-4 列(即 11 + 12 + 13)中的项目求和,我该如何指定?
如果我用手做,我会输入:
=SUM(B1:D1)
...但是我如何以编程方式动态生成该范围定义,只知道所需的行号 (1) 和列号 (2-4)?
=SUM(????)
预先感谢您的帮助!
(我使用的是 Microsoft Excel 2011 for Mac,因此 Excel VBA/基于宏的解决方案对我不起作用。)
我有同样的需求——看起来
OFFSET
功能可以让你做到这一点。
所以对于上述:
=SUM(OFFSET(A1,0,1,1,3))
分解它:
OFFSET(reference cell,
row offset from ref cell to start the range,
col offset to start the range, height of range you want,
width of range you want)
如果需要,您可以将偏移量设置为零,或者
+
向下引用,-
向上引用
这取决于如何引用“已知”行号和列号
例如,如果它们是工作表上单元格中的值:
A B
9 Row 1
10 ColStart 1
11 ColEnd 4
使用
INDRECT
函数构建范围参考
=SUM(INDIRECT("R"&B9&"C"&B10&":R"&B9&"C"&B11,FALSE))
不确定你想要什么。
你是这个意思吗?
dim parent_range as range
dim child_range as range
set parent_range = me.range("a1:e2")
set child_range = range(parent_range.rows(1).cells(2), parent_range.rows(1).cells(4))
msgbox "=sum(" & child_range.address(false, false, xla1) & ")"
或者你想要它作为一个公式?
=SUM(INDEX($A$1:$E$2,1,2):INDEX($A$1:$E$2,1,4))