LEN Function to Create Dynamic Array to Use for Named Ranges/Drop Down Lists

Squizza

New Member
Joined
May 31, 2022
Messages
8
Office Version
  1. 365
Hi, I am working with some source data is updated weekly, which has a list of Items in one column and Units in another column. There can be multiple instances of each item and the corresponding Units. I am trying to create a dynamic array of unique items in one column, and then in the adjacent columns, all the unique values associated with that item.

Source data example:

ITEMLOCATIONUNITS
AA 120G-08-1100
AA 120G-08-2100
AA 120G-12-1150
AA 60C-04-1350
BB 200D-11-236
BB 200G-19-130
BB 200G-17-230
SH 1001B-02-255
SH 1001B-03-155

What my goal is to have the data displayed as below, but with spilled data, not with a formula in the top cell and then dragged down to copy in all below cells.
AA 120100150
AA 60350
BB 2003036
SH 100155

I have refined a LET formula (below), that I found online to give me the all the unique values, but the Units don't spill, they are 'Text Joined' in one call and separated by a comma e.g AA 120 | 100,150 so on. That was the intent of the original LET formula I have been working from, but I can seem to work out how to make the unique unit values, spill instead of joining in the one cell.

Excel Formula:
=LET( data, $B$3#,
       item, INDEX(data,,1), units, INDEX(data,,3),
       uItem, TRANSPOSE(SORT(UNIQUE(item))),
       uUnits, SORT(UNIQUE(TRANSPOSE(IF(item=uItem,units,"")),TRUE),,,TRUE),
       uStr, LET( m, uUnits,
                   rSeq, SEQUENCE( ROWS(m) ),
                   L, MMULT( LEN(m)--(m<>""), SIGN( SEQUENCE( COLUMNS(m) ) ) ) - 1,
                   i, MMULT(--( TRANSPOSE( rSeq ) < rSeq ), L ) + rSeq,
                  IFERROR( MID( TEXTJOIN( ",", TRUE, m ), i, L ), "" ) ),
       CHOOSE( {1,2}, TRANSPOSE(uItem), uStr ) )

Any tips or assistance is greatly appreciated :)

Oh, may as well detail my end goal, maybe there is a better way. The source data (that updates weekly) is in an external workbook and is copied to $B$3#. On another sheet there is a complete list of all unique items, some that have no Units value. The end goal is to have a drop down list showing all unique Unit values for each item that has a Unit value on the source data. Like the below image in this example.

1654078637432.png


After I have the dynamic array, I will use VBA to update all named ranges for each Item, so that it refers to all the updated, corresponding unique Unit values.

VBA Code:
Sub RangeRename()

Dim n As Name
    For Each n In Names
        If Mid(n.RefersTo, 7) = "C" Then n.RefersTo = n.RefersTo & "#"
    Next n
End Sub

The VBA above is all I have so far. It only adds the # to the RefersTo cell, but I will add more code to first delete all named ranges, and then create named ranges from the dynamic range maybe something like this?

VBA Code:
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range

Set sht = Worksheets("Sheet1")
Set StartCell = Range("D9")

  LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
  LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column

  sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select

  Selection.CreateNames Top:=False, Left:=True, Bottom:=False, Right:=False

Not sure if it will all work, but love trying!

TIA
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi & welcome to MrExcel.
Whilst this is probably doable, all rows will be the same length, so in this instance they will all be 3 columns wide.
Also as it is a single formula, you may not be able to use it for named ranges & data validation.
 
Upvote 0
Thanks for the reply Fluff. So you can't see a way to generate a vertical spill of values and then variable length horizontal spills along each row?

The dynamic range created using the LET formula works well with named ranges and data validation (using =INDIRECT)

Do you know of a way for blanks to not show in the drop down list if they are in the referred range? The 'ignore blanks' does nothing it seems.

What should work at the least, is a single column dynamic range for the unique items, then an INDEX formula dragged down an extended amount of rows in the adjacent column. Then the VBA code to select the range based on a CTRL+Shift+Down, plus 1 column to the right. Could you see this being a possible solution?

Thanks again!
 
Upvote 0
Dragging a formula down would probably be the best bet, but you can play around with these options
++Fluff.xlsm
ABCDEFGHIJKLM
1ITEMLOCATIONUNITS
2AA 120G-08-1100AA 120100150AA 120100150
3AA 120G-08-2100AA 60350AA 60350
4AA 120G-12-1150BB 2003630BB 2003630
5AA 60C-04-1350SH 100155SH 100155
6BB 200D-11-236 
7BB 200G-19-130 
8BB 200G-17-230 
9SH 1001B-02-255
10SH 1001B-03-155
Summary
Cell Formulas
RangeFormula
E2:G5E2=LET(Data,A2:C10,u,UNIQUE(INDEX(Data,,1)),m,MAX(BYROW(u,LAMBDA(mr,COUNT(UNIQUE(FILTER(INDEX(Data,,3),INDEX(Data,,1)=mr)))))),s,SEQUENCE(,m+1),b,BYROW(u,LAMBDA(r,TEXTJOIN("|",,UNIQUE(FILTER(INDEX(Data,,3),INDEX(Data,,1)=r))))),x,MID(SUBSTITUTE(b,"|",REPT(" ",20)),SEQUENCE(,m,,20),20),IF(s=1,u,INDEX(x,{1;2;3;4},s-1)))
J2:J5J2=UNIQUE(A2:A10)
K2:L2,K4:L4,K3,K5:K8K2=IF(J2="","",TRANSPOSE(UNIQUE(FILTER($C$2:$C$10,$A$2:$A$10=J2))))
Dynamic array formulas.
 
Upvote 0
Thank you, will give them a go! What would you change the formula to in J2 for it to be a dynamic spill?

Also, couldn't see how to edit the post on my phone, realised the title has LEN instead of LET. Are you able to fix that as a mod?
 
Upvote 0
The formula in J2 is a spill array, it will spill down as many rows as needed.
 
Upvote 0
Sorry, I mean, the source range is dynamic and can be 200 rows or 600. So maybe =UNIQUE(A2:A1000)? But would this return a value for the blank cell/cells?
 
Upvote 0
Ok, you could use
Excel Formula:
=UNIQUE(FILTER(A2:A1000,A2:A1000<>""))
Also forgot to convert the numbers in E2 formula back to numbers, so it should be
Excel Formula:
=LET(Data,A2:C10,u,UNIQUE(INDEX(Data,,1)),m,MAX(BYROW(u,LAMBDA(mr,COUNT(UNIQUE(FILTER(INDEX(Data,,3),INDEX(Data,,1)=mr)))))),s,SEQUENCE(,m+1),b,BYROW(u,LAMBDA(r,TEXTJOIN("|",,UNIQUE(FILTER(INDEX(Data,,3),INDEX(Data,,1)=r))))),x,IFERROR(--MID(SUBSTITUTE(b,"|",REPT(" ",50)),SEQUENCE(,m,,50),50),""),IF(s=1,u,INDEX(x,{1;2;3;4},s-1)))
 
Upvote 0
I am almost finished with this, i have it all working, but my VBA needs to run across 3 sheets, and I can only get it working on the active sheet. Can anyone suggest how I can make the 2 Subs below run in the same Sub?

VBA Code:
Sub NSWUpdate()

Dim n As Name
    For Each n In Names
        If Mid(n.RefersTo, 7, 2) = "$Y" Then n.Delete
    Next n
Dim Ws As Worksheet
Dim LastRow As Long
Dim StartCell As Range

Set Ws = Worksheets("NSOH")
Set StartCell = Ws.Range("W12")
  
  Worksheets("NSOH").UsedRange
  LastRow = Ws.Cells(Ws.Rows.Count, StartCell.Column).End(xlUp).Row
  
    Ws.Range("X12:Y" & LastRow).Select
    Selection.CreateNames Top:=False, Left:=True, Bottom:=False, Right:=False
End Sub

VBA Code:
Sub VICUpdate()

Dim Ws As Worksheet
Dim LastRow As Long
Dim StartCell As Range

  Set Ws = Worksheets("VSOH")
  Set StartCell = Range("W12")
  
    LastRow = Ws.Cells(Ws.Rows.Count, StartCell.Column).End(xlUp).Row
  
    Ws.Range("X12:Y" & LastRow).Select
    Selection.CreateNames Top:=False, Left:=True, Bottom:=False, Right:=False

Dim n As Name
    For Each n In Names
        If Mid(n.RefersTo, 8, 1) = "Y" Then n.RefersTo = n.RefersTo & "#"
    Next n

End Sub

TIA
 
Upvote 0
Edited:
Deleted
Sorry misread the requirement
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top