Stack multi columns in one column for visible cells only

shafiey

Board Regular
Joined
Sep 6, 2023
Messages
60
Office Version
  1. 365
Platform
  1. Windows

Why doesn't the formula work in column B when I change the range from A2:A1012 to A2:A2100?​

Formula in B2:
=IFERROR(SORT(FILTERXML("<t><s>" & SUBSTITUTE(TEXTJOIN("، ",,IF(SUBTOTAL(103,OFFSET(A2,ROW(A2:A1012)-ROW(A2),)),A2:A1012,"")),"، ","</s><s>") & "</s></t>","//s")),"")

Formula in C2:
=IFERROR(SORT(FILTERXML("<t><s>" & SUBSTITUTE(TEXTJOIN("، ",,IF(SUBTOTAL(103,OFFSET(A2,ROW(A2:A2100)-ROW(A2),)),A2:A2100,"")),"، ","</s><s>") & "</s></t>","//s")),"")

Why doesn't the formula work in column B when I change the range from A2:A1012 to A2:A2100?

My File

Thank you very much
 
In the sample below, I have filled in the blank cell B3 and changed the yellow cell as shown.
I have also reduced the ranges so that no blank cells are involved.
If you do that what results do you get? As you can see, for me the results are missing the "x" in both lists.

shafiey.xlsm
ABCDEF
1Col01Col02Col03Col04Duplicateswithout Duplicates
2ab, cd, e, fg, h, i, jaa
3ab, cd, e, fg, h, i, jab
4ab, cd, e, fg, h, i, jac
5ab, cd, e, fg, h, i, jad
6ab, cd, e, fg, h, i, jae
7ab, cd, e, fg, h, i, jaf
8ab, cd, e, fg, h, i, jag
9ab, cd, e, fg, h, i, jah
10ab, cd, e, fg, h,x, jai
11ab, cd, e, fg, h, i, jaj
12b
13b
14b
15b
16b
17b
18b
19b
20b
21b
22c
23c
24c
25c
26c
27c
28c
29c
30c
31c
32d
33d
34d
35d
36d
37d
38d
39d
40d
41d
42e
43e
44e
45e
46e
47e
48e
49e
50e
51e
52f
53f
54f
55f
56f
57f
58f
59f
60f
61f
62g
63g
64g
65g
66g
67g
68g
69g
70g
71g
72h
73h
74h
75h
76h
77h
78h
79h
80h
81h
82i
83i
84i
85i
86i
87i
88i
89i
90i
91i
92j
93j
94j
95j
96j
97j
98j
99j
100j
101j
102
Sheet4
Cell Formulas
RangeFormula
E2:E101E2=LET( a,FILTER(A2:D11,SUBTOTAL(3,OFFSET(A2,ROW(A2:A11)-ROW(A2),0,1))), b,INDEX(a,MOD(SEQUENCE(COUNTA(a))-1,ROWS(a))+1,MOD(SEQUENCE(COUNTA(a))-1,4)+1), c,LEN(b)-LEN(SUBSTITUTE(b,",",""))+1, d,MMULT(--(SEQUENCE(ROWS(c))>=TRANSPOSE(SEQUENCE(ROWS(c)))),c), e,SEQUENCE(MAX(d)), f,XLOOKUP(e,d,d,,1), g,XMATCH(e,d,1), SORT(FILTERXML("<t><s>" & SUBSTITUTE(INDEX(b,g),",","</s><s>") & "</s></t>","//s["&f-e+1&"]")))
F2:F11F2=LET( a,FILTER(A2:D11,SUBTOTAL(3,OFFSET(A2,ROW(A2:A11)-ROW(A2),0,1))), b,INDEX(a,MOD(SEQUENCE(COUNTA(a))-1,ROWS(a))+1,MOD(SEQUENCE(COUNTA(a))-1,4)+1), c,LEN(b)-LEN(SUBSTITUTE(b,",",""))+1, d,MMULT(--(SEQUENCE(ROWS(c))>=TRANSPOSE(SEQUENCE(ROWS(c)))),c), e,SEQUENCE(MAX(d)), f,XLOOKUP(e,d,d,,1), g,XMATCH(e,d,1), SORT(UNIQUE(FILTERXML("<t><s>" & SUBSTITUTE(INDEX(b,g),",","</s><s>") & "</s></t>","//s["&f-e+1&"]"))))
Dynamic array formulas.
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
In the sample below, I have filled in the blank cell B3 and changed the yellow cell as shown.
I have also reduced the ranges so that no blank cells are involved.
If you do that what results do you get? As you can see, for me the results are missing the "x" in both lists.

shafiey.xlsm
ABCDEF
1Col01Col02Col03Col04Duplicateswithout Duplicates
2ab, cd, e, fg, h, i, jaa
3ab, cd, e, fg, h, i, jab
4ab, cd, e, fg, h, i, jac
5ab, cd, e, fg, h, i, jad
6ab, cd, e, fg, h, i, jae
7ab, cd, e, fg, h, i, jaf
8ab, cd, e, fg, h, i, jag
9ab, cd, e, fg, h, i, jah
10ab, cd, e, fg, h,x, jai
11ab, cd, e, fg, h, i, jaj
12b
13b
14b
15b
16b
17b
18b
19b
20b
21b
22c
23c
24c
25c
26c
27c
28c
29c
30c
31c
32d
33d
34d
35d
36d
37d
38d
39d
40d
41d
42e
43e
44e
45e
46e
47e
48e
49e
50e
51e
52f
53f
54f
55f
56f
57f
58f
59f
60f
61f
62g
63g
64g
65g
66g
67g
68g
69g
70g
71g
72h
73h
74h
75h
76h
77h
78h
79h
80h
81h
82i
83i
84i
85i
86i
87i
88i
89i
90i
91i
92j
93j
94j
95j
96j
97j
98j
99j
100j
101j
102
Sheet4
Cell Formulas
RangeFormula
E2:E101E2=LET( a,FILTER(A2:D11,SUBTOTAL(3,OFFSET(A2,ROW(A2:A11)-ROW(A2),0,1))), b,INDEX(a,MOD(SEQUENCE(COUNTA(a))-1,ROWS(a))+1,MOD(SEQUENCE(COUNTA(a))-1,4)+1), c,LEN(b)-LEN(SUBSTITUTE(b,",",""))+1, d,MMULT(--(SEQUENCE(ROWS(c))>=TRANSPOSE(SEQUENCE(ROWS(c)))),c), e,SEQUENCE(MAX(d)), f,XLOOKUP(e,d,d,,1), g,XMATCH(e,d,1), SORT(FILTERXML("<t><s>" & SUBSTITUTE(INDEX(b,g),",","</s><s>") & "</s></t>","//s["&f-e+1&"]")))
F2:F11F2=LET( a,FILTER(A2:D11,SUBTOTAL(3,OFFSET(A2,ROW(A2:A11)-ROW(A2),0,1))), b,INDEX(a,MOD(SEQUENCE(COUNTA(a))-1,ROWS(a))+1,MOD(SEQUENCE(COUNTA(a))-1,4)+1), c,LEN(b)-LEN(SUBSTITUTE(b,",",""))+1, d,MMULT(--(SEQUENCE(ROWS(c))>=TRANSPOSE(SEQUENCE(ROWS(c)))),c), e,SEQUENCE(MAX(d)), f,XLOOKUP(e,d,d,,1), g,XMATCH(e,d,1), SORT(UNIQUE(FILTERXML("<t><s>" & SUBSTITUTE(INDEX(b,g),",","</s><s>") & "</s></t>","//s["&f-e+1&"]"))))
Dynamic array formulas.
Thank you very much for your attention. I had not thought of this.
In this way, I think the formula has a lot of work to do?
Is there any other solution?
 
Upvote 0
I had not thought of this.
It was mentioned way back in posts 18 & 19 ;)
... more than 2,000 words were missing from the results.

about 500 out of 4,300 words missing.


Is there any other solution?
Yes, vba, also mentioned before. This time I have used a User-Defined Function (UDF). I have still also employed an extra column. This could be achieved with vba and without the helper column but the function would be slower to calculate with large ranges. Post back if you want that option though.

Data sheet with the helper column

shafiey.xlsm
ABCDE
1Col01Col02Col03Col04
2ab, cd, e, fg, h, i, j4
3ad, e, fg, h, i, j3
4ab, cd, e, fg, h, i, j4
50
6ab, cd, e, fg, h, i, j4
7ab, cd, e, fg, h, i, j4
8b, cd, e, fg, h, i, j3
9ab, cd, e, fg, h, i, j4
10ab, cd, e, fg, h,x, j4
11ab, cd, e, fg, h, i, j4
Sheet1
Cell Formulas
RangeFormula
E2:E11E2=SUBTOTAL(103,A2:D2)


To implement the UDF
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below (top cell in each column only)
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function ListItems(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim a As Variant, itm As Variant
  Dim i As Long, cols As Long
 
  a = r.Value
  cols = UBound(a, 2)
  With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(a)
      If a(i, cols) > 0 Then
        For Each itm In Split(Replace(Application.TextJoin(",", 1, Application.Index(a, i, Application.Sequence(cols - 1))), " ", ""), ",")
          If RemoveDupes Then
            If Not .Contains(itm) Then .Add itm
          Else
            .Add itm
          End If
        Next itm
      End If
    Next i
    .Sort
    If .Count > 0 Then ListItems = Application.Transpose(.ToArray)
  End With
End Function

shafiey_2.xlsm
AB
1Duplicateswithout Duplicates
2aa
3ab
4ac
5ad
6ae
7af
8ag
9ah
10bi
11bj
12bx
13b
14b
15b
16b
17b
18c
19c
20c
21c
22c
23c
24c
25c
26d
27d
28d
29d
30d
31d
32d
33d
34d
35e
36e
37e
38e
39e
40e
41e
42e
43e
44f
45f
46f
47f
48f
49f
50f
51f
52f
53g
54g
55g
56g
57g
58g
59g
60g
61g
62h
63h
64h
65h
66h
67h
68h
69h
70h
71i
72i
73i
74i
75i
76i
77i
78i
79j
80j
81j
82j
83j
84j
85j
86j
87j
88x
89
Sheet2
Cell Formulas
RangeFormula
A2:A88A2=ListItems(Sheet1!A2:E11,FALSE)
B2:B12B2=ListItems(Sheet1!A2:E11,TRUE)
Dynamic array formulas.
 
Last edited:
Upvote 0
It was mentioned way back in posts 18 & 19 ;)






Yes, vba, also mentioned before. This time I have used a User-Defined Function (UDF). I have still also employed an extra column. This could be achieved with vba and without the helper column but the function would be slower to calculate with large ranges. Post back if you want that option though.

Data sheet with the helper column

shafiey.xlsm
ABCDE
1Col01Col02Col03Col04
2ab, cd, e, fg, h, i, j4
3ad, e, fg, h, i, j3
4ab, cd, e, fg, h, i, j4
50
6ab, cd, e, fg, h, i, j4
7ab, cd, e, fg, h, i, j4
8b, cd, e, fg, h, i, j3
9ab, cd, e, fg, h, i, j4
10ab, cd, e, fg, h,x, j4
11ab, cd, e, fg, h, i, j4
Sheet1
Cell Formulas
RangeFormula
E2:E11E2=SUBTOTAL(103,A2:D2)


To implement the UDF
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below (top cell in each column only)
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function ListItems(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim a As Variant, itm As Variant
  Dim i As Long, cols As Long
 
  a = r.Value
  cols = UBound(a, 2)
  With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(a)
      If a(i, cols) > 0 Then
        For Each itm In Split(Replace(Application.TextJoin(",", 1, Application.Index(a, i, Application.Sequence(cols - 1))), " ", ""), ",")
          If RemoveDupes Then
            If Not .Contains(itm) Then .Add itm
          Else
            .Add itm
          End If
        Next itm
      End If
    Next i
    .Sort
    If .Count > 0 Then ListItems = Application.Transpose(.ToArray)
  End With
End Function

shafiey.xlsm
AB
1Duplicateswithout Duplicates
2aa
3ab
4ac
5ad
6ae
7af
8ag
9ah
10ai
11bj
12bx
13b
14b
15b
16b
17b
18b
19c
20c
21c
22c
23c
24c
25c
26c
27d
28d
29d
30d
31d
32d
33d
34d
35d
36e
37e
38e
39e
40e
41e
42e
43e
44e
45f
46f
47f
48f
49f
50f
51f
52f
53f
54g
55g
56g
57g
58g
59g
60g
61g
62g
63h
64h
65h
66h
67h
68h
69h
70h
71h
72i
73i
74i
75i
76i
77i
78i
79i
80j
81j
82j
83j
84j
85j
86j
87j
88j
89x
90
Sheet2
Cell Formulas
RangeFormula
A2:A89A2=ListItems(Sheet1!A2:E11,FALSE)
B2:B12B2=ListItems(Sheet1!A2:E11,TRUE)
Dynamic array formulas.
I did all the steps you said, but it gives an error. I don't know which part I did wrong. I have sent the file, please check it if possible.
MyFile
 
Upvote 0
You have not entered the formulas in the helper column. See column E in the first mini sheet in post 33.

Also, it would not be a good idea to put the formula beside the data like that. If you filter the original data to hide some rows, it will also hide some rows of the results.
 
Upvote 0
You have not entered the formulas in the helper column. See column E in the first mini sheet in post 33.

Also, it would not be a good idea to put the formula beside the data like that. If you filter the original data to hide some rows, it will also hide some rows of the results.
I corrected it, but it does not show the results.
File02
 
Upvote 0
I corrected it
No you haven't. You have not entered the formulas in column E.

1694168203894.png
 
Upvote 0
No you haven't. You have not entered the formulas in column E.

View attachment 98424
wowwwww :biggrin: , very very good. I tested the formula and all the results were correct. The processing speed was also excellent. My problem was solved. Thank you very much for your efforts. I wish you success and health.
 
Upvote 0
You're welcome. Glad we seem to have got there in the end. 😎
 
Upvote 0
It was mentioned way back in posts 18 & 19 ;)






Yes, vba, also mentioned before. This time I have used a User-Defined Function (UDF). I have still also employed an extra column. This could be achieved with vba and without the helper column but the function would be slower to calculate with large ranges. Post back if you want that option though.

Data sheet with the helper column

shafiey.xlsm
ABCDE
1Col01Col02Col03Col04
2ab, cd, e, fg, h, i, j4
3ad, e, fg, h, i, j3
4ab, cd, e, fg, h, i, j4
50
6ab, cd, e, fg, h, i, j4
7ab, cd, e, fg, h, i, j4
8b, cd, e, fg, h, i, j3
9ab, cd, e, fg, h, i, j4
10ab, cd, e, fg, h,x, j4
11ab, cd, e, fg, h, i, j4
Sheet1
Cell Formulas
RangeFormula
E2:E11E2=SUBTOTAL(103,A2:D2)


To implement the UDF
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below (top cell in each column only)
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function ListItems(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim a As Variant, itm As Variant
  Dim i As Long, cols As Long
 
  a = r.Value
  cols = UBound(a, 2)
  With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(a)
      If a(i, cols) > 0 Then
        For Each itm In Split(Replace(Application.TextJoin(",", 1, Application.Index(a, i, Application.Sequence(cols - 1))), " ", ""), ",")
          If RemoveDupes Then
            If Not .Contains(itm) Then .Add itm
          Else
            .Add itm
          End If
        Next itm
      End If
    Next i
    .Sort
    If .Count > 0 Then ListItems = Application.Transpose(.ToArray)
  End With
End Function

shafiey.xlsm
AB
1Duplicateswithout Duplicates
2aa
3ab
4ac
5ad
6ae
7af
8ag
9ah
10ai
11bj
12bx
13b
14b
15b
16b
17b
18b
19c
20c
21c
22c
23c
24c
25c
26c
27d
28d
29d
30d
31d
32d
33d
34d
35d
36e
37e
38e
39e
40e
41e
42e
43e
44e
45f
46f
47f
48f
49f
50f
51f
52f
53f
54g
55g
56g
57g
58g
59g
60g
61g
62g
63h
64h
65h
66h
67h
68h
69h
70h
71h
72i
73i
74i
75i
76i
77i
78i
79i
80j
81j
82j
83j
84j
85j
86j
87j
88j
89x
90
Sheet2
Cell Formulas
RangeFormula
A2:A89A2=ListItems(Sheet1!A2:E11,FALSE)
B2:B12B2=ListItems(Sheet1!A2:E11,TRUE)
Dynamic array formulas.
In post 33, you mentioned that it is possible without the helper column, but its speed is lower. I would be grateful if you could send me that method so that I can test it. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,119
Members
452,381
Latest member
Nova88

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