如何在Excel工作表中循环使用FormulaArrays

Excel 2010.我需要找出FormulaArrays在表单中的位置。 特别是我需要得到他们的地址。

我不知道一个给定的(1×1)单元格是“ArrayArray”的一部分,而是知道FormulaArray“blocks”所在的位置。 例如,如果在A1:B2中input了一个公式数组,我想要得到公式数组区域超出范围A1:B2的信息,而不是知道单个单元格A1,A2,B1,B2是“一个“公式arrays”。

这可能吗?

这并不像我希望的那么容易。 Excel似乎不公开您的数组公式的集合,以循环。 这意味着你必须检查每个单元格。 在高层次这个代码:

  1. 扫描每个细胞。
  2. 如果单元格包含数组公式,则会提取地址。
  3. 如果地址是新的,则将其添加到地址variables中。
  4. 将所有地址输出到屏幕。

    ' Returns the address of array formula in the current worksheet. Sub GetArrayFormulaRangeAddresses() Dim r As Range ' Used to loop over active cells. Dim addresses() As String ' Holds each found address. Dim address As String ' Used to avoid duplicate entries. Dim foundCount As Integer ' Count of found array formulas. ' Initialise vars. foundCount = -1 ' Check every active cell, in currently selected tab. ' Ideally you would loop over a formula collection but ' the Excel object model does not appear to expose this. For Each r In ActiveSheet.UsedRange.Cells ' Found one. ' WARNING: Array formula contains values, ' even when cell is not part of an array formula. If r.FormulaArray Like "={*}" Then ' WARNING: Cannot pass array until after firt redim statement. ' To avoid check found count, then addresses array. If foundCount = -1 Then ' Not found, add to list. foundCount = foundCount + 1 ReDim Preserve addresses(foundCount) addresses(foundCount) = r.CurrentArray.address Else ' Check if address already found. If Not CheckArrayContains(addresses, r.CurrentArray.address) Then ' Not found, add to list. foundCount = foundCount + 1 ReDim Preserve addresses(foundCount) addresses(foundCount) = r.CurrentArray.address End If End If End If Next ' TODO: Action you output as necessary! For foundCount = LBound(addresses) To UBound(addresses) MsgBox addresses(foundCount) Next End Sub ' Returns true if the passed array contains a value. ' Otherwise returns false. Public Function CheckArrayContains(ByRef CheckArray() As String, ByVal CheckValue As String) As Boolean Dim i As Integer ' Counter, used to check each element of the array. ' Check existing members. For i = LBound(CheckArray) To UBound(CheckArray) If CheckArray(i) = CheckValue Then ' Match found. CheckArrayContains = True Exit Function End If Next ' No match found. CheckArrayContains = False End Function 

我认为这个代码可以改进:

  1. Redim很贵。 这可能会更好地调整数组的1000个批次。
  2. Excel对象模型 可能会公开可以循环的集合。
  3. 对于较大的工作簿,提取所有地址并重复删除结果会更快。