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 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
Here is another UDF that does that. I doubt you will notice any difference in speed with the sample data but with thousands of rows and multiple columns with multiple words you may well see a slowness.

VBA Code:
Function ItemList(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim AL As Object
  Dim itm As Variant
  Dim c As Range, rw As Range

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each rw In r.Rows
    If Not rw.EntireRow.Hidden Then
      For Each c In rw.Cells
        For Each itm In Split(Replace(c.Value, " ", ""), ",")
          If RemoveDupes Then
            If Not AL.Contains(itm) Then AL.Add itm
          Else
            AL.Add itm
          End If
        Next itm
      Next c
    End If
  Next rw
  AL.Sort
  If AL.Count > 0 Then ItemList = Application.Transpose(AL.ToArray)
End Function

Results (same Sheet1 data as before but no need for the helper column)

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=ItemList(Sheet1!A2:D11,FALSE)
B2:B12B2=ItemList(Sheet1!A2:D11,TRUE)
Dynamic array formulas.
 
Last edited:
Upvote 0
Solution

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Here is another UDF that does that. I doubt you will notice any difference in speed with the sample data but with thousands of rows and multiple columns with multiple words you may well see a slowness.

VBA Code:
Function ItemList(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim AL As Object
  Dim itm As Variant
  Dim c As Range, rw As Range

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each rw In r.Rows
    If Not rw.EntireRow.Hidden Then
      For Each c In rw.Cells
        For Each itm In Split(Replace(c.Value, " ", ""), ",")
          If RemoveDupes Then
            If Not AL.Contains(itm) Then AL.Add itm
          Else
            AL.Add itm
          End If
        Next itm
      Next c
    End If
  Next rw
  AL.Sort
  If AL.Count > 0 Then ItemList = Application.Transpose(AL.ToArray)
End Function

Results (same Sheet1 data as before but no need for the helper column)

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=ItemList(Sheet1!A2:D11,FALSE)
B2:B12B2=ItemList(Sheet1!A2:D11,TRUE)
Dynamic array formulas.
Oh my god, this one is great. This is exactly what I was looking for. Thank you for your kindness.🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏
 
Upvote 0
Have you tried it on the large data yet?
Yes, I ran your VBA code on a column with 2100 rows and with different iterations (2-7 iterations per row) and the results were correct. The processing speed was also very high and within 1 second (one click).
 
Upvote 0
Hello, I also tested the data using VSTAVK and TEXTSPLIT functions in Office 365 and the result was the same as your VBA code.
Just be aware that (as per some of the previous examples) those results may not be reliable. And if your data is large any incorrect results will not be obvious.
 
Upvote 0
Here is another UDF that does that. I doubt you will notice any difference in speed with the sample data but with thousands of rows and multiple columns with multiple words you may well see a slowness.

VBA Code:
Function ItemList(r As Range, Optional RemoveDupes As Boolean = False) As Variant
  Dim AL As Object
  Dim itm As Variant
  Dim c As Range, rw As Range

  Set AL = CreateObject("System.Collections.ArrayList")
  For Each rw In r.Rows
    If Not rw.EntireRow.Hidden Then
      For Each c In rw.Cells
        For Each itm In Split(Replace(c.Value, " ", ""), ",")
          If RemoveDupes Then
            If Not AL.Contains(itm) Then AL.Add itm
          Else
            AL.Add itm
          End If
        Next itm
      Next c
    End If
  Next rw
  AL.Sort
  If AL.Count > 0 Then ItemList = Application.Transpose(AL.ToArray)
End Function

Results (same Sheet1 data as before but no need for the helper column)

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=ItemList(Sheet1!A2:D11,FALSE)
B2:B12B2=ItemList(Sheet1!A2:D11,TRUE)
Dynamic array formulas.
Hello, in this function, how can the resulting data be sorted in the column.
I added the sort function to the formula, but it didn't work:
=SORT(ItemList(D2:D6000),FALSE))
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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