简体   繁体   English

Excel中的VBA样式范围选择

[英]VBA-style range selection in Excel

In a spreadsheet formula, (namely SumIfs ) I would like to be able to select a column range using its delimiters instead of a A1:A3456 style, like I would do in VBA [Range("A1:A3456")~Range(Cells(1,1),Cells(3456,1))] . 在电子表格公式中(即SumIfs ),我希望能够使用其定界符而不是A1:A3456样式来选择列范围,就像我在VBA [Range("A1:A3456")~Range(Cells(1,1),Cells(3456,1))]

If this is not possible, is there any workaround to use the result of a 'Match' function to get the column number where I want to apply certain criteria? 如果这不可能,是否有任何解决方法可使用“匹配”函数的结果来获取我要应用某些条件的列号?

My function would look like 我的功能看起来像

=Sumifs(A2:A10;*{range(cells(1,match(Z5;A1:T1)), cell(10,match(Z5;A1:T1))}*,"="&1)

...if only I could add vba formulas inside the spreadsheet. ...如果我可以在电子表格中添加vba公式。

Use Offset, which is much richer as a worksheet function than it is in VBA. 使用偏移量,它比VBA中的工作表功能丰富得多。

Syntax 句法

OFFSET(reference, rows, cols, [height], [width]) 偏移量(参考,行,列,[高度],[宽度])

The OFFSET function syntax has the following arguments: OFFSET函数语法具有以下参数:

Reference Required. 需要参考。 The reference from which you want to base the offset. 您要作为偏移量基准的参考。 Reference must refer to a cell or range of adjacent cells; 引用必须指一个单元格或相邻单元格的范围; otherwise, OFFSET returns the #VALUE! 否则,OFFSET返回#VALUE! error value. 错误值。

Rows Required. 必填行。 The number of rows, up or down, that you want the upper-left cell to refer to. 您希望左上方单元格引用的行数(上下)。 Using 5 as the rows argument specifies that the upper-left cell in the reference is five rows below reference. 使用5作为rows参数指定引用中的左上单元格在引用下方五行。 Rows can be positive (which means below the starting reference) or negative (which means above the starting reference). 行可以是正数(表示在起始引用下方)或负数(表示在起始引用上方)。

Cols Required. 必需的列。 The number of columns, to the left or right, that you want the upper-left cell of the result to refer to. 您要引用结果的左上角单元格的左侧或右侧的列数。 Using 5 as the cols argument specifies that the upper-left cell in the reference is five columns to the right of reference. 使用5作为cols参数指定参考中的左上单元格在参考的右侧五列。 Cols can be positive (which means to the right of the starting reference) or negative (which means to the left of the starting reference). Cols可以为正(表示起始参考的右侧)或负(表示起始参考的左侧)。

Height Optional. 高度可选。 The height, in number of rows, that you want the returned reference to be. 您希望返回的引用成为的高度(以行数为单位)。 Height must be a positive number. 高度必须为正数。

Width Optional. 宽度可选。 The width, in number of columns, that you want the returned reference to be. 您希望返回的引用的宽度(以列数为单位)。 Width must be a positive number. 宽度必须为正数。

from https://support.office.com/en-us/article/OFFSET-function-c8de19ae-dd79-4b9b-a14e-b4d906d11b66 来自https://support.office.com/en-us/article/OFFSET-function-c8de19ae-dd79-4b9b-a14e-b4d906d11b66

You can use a combination of Address and Indirect , like this: 您可以结合使用AddressIndirect ,如下所示:

=SUM(INDIRECT(ADDRESS(1,1) & ":" & ADDRESS(3456,1)))

which is equivalent to =SUM(A1:A3456) 等效于=SUM(A1:A3456)

ADRESS transforms from style Cells(i,j) to style A1 . ADRESS从样式Cells(i,j)为样式A1

INDIRECT serves to interpret the resulting string as a range address. INDIRECT用于将结果字符串解释为范围地址。

SUM here is an example of usage. 这里的SUM是用法示例。

Well It's a bit ugly, but if you want to do that occasionally and don't want to switch completely to RC-style , it's probably the easiest way. 好吧,这有点丑陋,但是如果您偶尔要这样做并且不想完全转换为RC-style ,那可能是最简单的方法。

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

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