简体   繁体   中英

Excel Array formula using INDEX and is variable dependant

I currently have a sheet where data is being entered into specific columns. Then within a summary sheet, I am using an array formula with an index to generate a list of all data on the original sheet matching a specific field.

Each column on the summary sheet has an array formula, linking back to the equivalent column on the data sheet. A->A, B->B, C->C etc...

The array formula is as follows:

{=IFERROR(INDEX('data'!C$2:C$1168,SMALL(IF('data'!$S$2:$S$1168>='OUTPUT'!$A$2,ROW('data'!C$2:C$1168)-ROW('data'!C$2)+1),ROWS('data'!C$2:'data'!C2))),"")}

This formula works perfectly and generates a live updating list of all rows where the value of cell column "S", in sheet "data", is "OUTPUT".

Sheet "data":

|Sample A|Result A|OUTPUT|
|Sample B|Result B|0     |
|Sample C|Result C|OUTPUT|

Result on the "summary" sheet:

|Sample A|Result A|
|Sample C|Result C|

Within the sheet "data" however, there is now also a column for quantity, and I would like to attempt to have that reflected in the "summary" sheet. As follows:

Sheet "data":

|Sample A|Result A|OUTPUT|  2  |
|Sample B|Result B|0     |  5  |
|Sample C|Result C|OUTPUT|  3  |

Result on the "summary" sheet:

|Sample A|Result A|
|Sample A|Result A|
|Sample C|Result C|
|Sample C|Result C|
|Sample C|Result C|

Any advice or recommendations on how to cause this "loop" with an array would be much appreciated. I would typically write a macro to handle the data with a function that loops the output whenever needed, however my objective is to avoid having to run a macro each time new data is added.

Thank you and regards

Excel formulas are nice, but I tend to use UDFs in conjunction with Named Ranges in this situation.

Use a UDF to return an array of all the desired out strings ONCE, and put into a Named Range. Then call INDEX on your named range.

UDF:

Public Function getResultArray( _
       sampleRange As Range, _
       isOutputRange As Range, _
       QuantityRange As Range, _
       str as string) As String()

  'error handling for inputs here
  Dim totalQuantity As Long
  totalQuantity = Application.WorksheetFunction.SumIf(isOutputRange, str, QuantityRange)

  Dim retArr() As String
  ReDim retArr(1 To totalQuantity)

  Dim ioArr As Variant, qArr As Variant, sampleArr As Variant
  ioArr = isOutputRange.Value
  qArr = QuantityRange.Value
  sampleArr = sampleRange.Value

  Dim i As Long, j As Long, counter as Long
  For i = LBound(ioArr) To UBound(ioArr)
        If ioArr(i, 1) = str Then
              For j = 1 To qArr(i, 1)
                    counter = counter + 1
                    retArr(counter) = sampleArr(i, 1)
              Next j
        End If
  Next i

  getResultArray = retArr
End Function

Named Range:

ResultArray = getResultArray(data!$A$2:$A$4, data!$C$2:$C$4, data!$D$2:$D$4, OUTPUT!$A$2)

Where you want the 'summary':

=INDEX(ResultArray, ROWS('data'!C$2:'data'!C2))

This cuts down on workbook calculation, as the arrays are only calculated once, and not for each worksheet formula(array formulas are expensive)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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