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
 
Either way, you could give this a try.
I have assumed that the data is on Sheet1 and results go onto Sheet2 which exists but has nothing on it to start with.

On Sheet1 add the formula shown in C2 and copy down at least to the bottom of the data. Doesn't matter if the formula goes further as I have done.

shafiey.xlsm
ABC
1keywordskeywords02
2ah2
3h,kd, i, g2
4ah, h2
5b, d, g, h, ib, e, b2
6gd2
70
80
Sheet1
Cell Formulas
RangeFormula
C2:C8C2=SUBTOTAL(103,A2:B2)


Now run the following macro. Post back if you need instructions about that.

VBA Code:
Sub Make_List()
  Dim a As Variant, b As Variant, itm As Variant
  Dim i As Long
 
  With Sheets("Sheet1")
    a = .Range("A2:C" & .Columns("A:B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row).Value
  End With
  With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(a)
      If a(i, 3) > 0 Then
        For Each itm In Split(Replace(Application.TextJoin(",", 1, a(i, 1), a(i, 2)), " ", ""), ",")
          If Not .Contains(itm) Then .Add itm
        Next itm
      End If
    Next i
    .Sort
    If .Count > 0 Then b = Application.Transpose(.ToArray)
  End With
  With Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Offset(, 1)
    .Value = .Offset(, -1).Value + 1
    If Not IsEmpty(b) Then .Offset(1).Resize(UBound(b) - LBound(b) + 1).Value = b
  End With
End Sub

Filter the data differently and run the macro again.
Each time the macro is run, the results are written to Sheet2 in a new column (the location can be changed to suit your requirements).
Here is my Sheet2 after I have run the macro 5 times. The top row just counts the number of times the macro has been run & could be removed from the code if not required.
  1. First run was with nothing filtered out.
  2. Second run I filtered out the "a" in column A
  3. Third run I returned column A to Show All and filtered column B to show "d" only
  4. Fourth run I returned both columns to Show All but then applied a text filter to show "x" in column B. This meant all rows except the heading row were hidden.
  5. Fifth run I returned both columns to Show All
shafiey.xlsm
ABCDEF
112345
2abda
3bdgb
4ded
5ege
6ghg
7hih
8iki
9kk
10
Sheet2
That's OK. very very thanks
The extendoffice.com site has also put the following code on their site, which solves the problem.
Is it possible to re-edit the code so that the repetitions are also given in the column? similar to the image below:

Sub Vertical()
'UpdatebyExtendoffice20161125
Dim i As Long, strTxt As String
Dim startP As Range
Dim xRg As Range, yRg As Range
On Error Resume Next
Set xRg = Application.InputBox _
(Prompt:="Range Selection...", _
Title:="Kutools For Excel", Type:=8)
i = 1
Application.ScreenUpdating = False
For Each yRg In xRg
If i = 1 Then
strTxt = yRg.Text
i = 2
Else
strTxt = strTxt & "¡ " & yRg.Text
End If
Next
Application.ScreenUpdating = True
Set startP = Application.InputBox _
(Prompt:="paste range...", _
Title:="Kutools For Excel", Type:=8)
ary = Split(strTxt, "¡ ")
i = 1
Application.ScreenUpdating = False
For Each a In ary
startP(i, 1).Value = a
i = i + 1
Next a
Application.ScreenUpdating = True
End Sub
 

Attachments

  • With Duplicates.jpg
    With Duplicates.jpg
    140.5 KB · Views: 7
Last edited by a moderator:
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
wow, That's ok. very very great, your formula worked.
Thank you very much.
Thank you very much for taking the time for me.
Is it possible to include repetitions in the column? Same as the following image:
Just remove the UNIQUE() function from the last row.

Slightly surprised you had no issues using it, but all the better, I guess.
 
Upvote 0
Can you please stop quoting the same thing multiple times? I have just removed 3 identical quotes from your last two posts! :(

There is also no need to keep asking the same question as you have done at the bottom of posts 20 and 21.

wow, That's ok. very very great, your formula worked.
As you will have gathered from the discussion in the thread, that formula works on your small sample data, but it almost certainly will not work reliably on the large data that you said you had.
To be sure, you would need to check manually that nothing had been missed - and that would defeat the purpose of having a formula in the first place.

The extendoffice.com site has also put the following code on their site, which solves the problem.
If you are using code from another site, perhaps you should ask them about modifications.

Did you try the method that I suggested in post 16?
 
Upvote 0
Can you please stop quoting the same thing multiple times? I have just removed 3 identical quotes from your last two posts! :(

There is also no need to keep asking the same question as you have done at the bottom of posts 20 and 21.

As you will have gathered from the discussion in the thread, that formula works on your small sample data, but it almost certainly will not work reliably on the large data that you said you had.
To be sure, you would need to check manually that nothing had been missed - and that would defeat the purpose of having a formula in the first place.


If you are using code from another site, perhaps you should ask them about modifications.

Did you try the method that I suggested in post 16?
Yes, I run your vba code, but it takes a long time. My system RAM is 6GB and I have run it since 17:05 for 2100 rows but till now 17:24 is processing.
 
Upvote 0
Can you please stop quoting the same thing multiple times? I have just removed 3 identical quotes from your last two posts! :(

There is also no need to keep asking the same question as you have done at the bottom of posts 20 and 21.

As you will have gathered from the discussion in the thread, that formula works on your small sample data, but it almost certainly will not work reliably on the large data that you said you had.
To be sure, you would need to check manually that nothing had been missed - and that would defeat the purpose of having a formula in the first place.


If you are using code from another site, perhaps you should ask them about modifications.

Did you try the method that I suggested in post 16?
I tested your vba code for 1936 rows with 362 words with different iterations, unfortunately it returned only 129 words.
 
Upvote 0
Just remove the UNIQUE() function from the last row.

Slightly surprised you had no issues using it, but all the better, I guess.
When I delete some cells in columns A to D, the results of the number of repetitions in column E return wrong.

My Test file

test.xlsx
ABCDEF
1Col01Col02Col03Col04Duplicateswithout Duplicates
2ab, cd, e, fg, h, i, jaa
3ad, 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, i, jai
11ab, cd, e, fg, h, i, jaj
12b#VALUE!
13b
14b
15b
16b
17b
18b
19b
20c
21c
22c
23c
24c
25c
26c
27c
28d
29d
30d
31d
32d
33d
34d
35d
36d
37d
38e
39e
40e
41e
42e
43e
44e
45e
46e
47e
48f
49f
50f
51f
52f
53f
54f
55f
56f
57f
58g
59g
60g
61g
62g
63g
64g
65g
66g
67g
68h
69h
70h
71h
72h
73h
74h
75h
76h
77h
78i
79i
80i
81i
82i
83i
84i
85i
86i
87i
88j
89j
90j
91j
92j
93j
94j
95j
96j
97j
98#VALUE!
99#VALUE!
100
Sheet1
Cell Formulas
RangeFormula
E2:E99E2=LET( a,FILTER(A2:D20,SUBTOTAL(3,OFFSET(A2,ROW(A2:A20)-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:F12F2=LET( a,FILTER(A2:D20,SUBTOTAL(3,OFFSET(A2,ROW(A2:A20)-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
You have not encountered this issue yet, but it cannot handle blanks in column A; by nature of using SUBTOTAL(3). I have no idea how that would be worked around.
As for this error, wrapping FILTERXML with IFERROR(,"") should suffice.
 
Upvote 0
You have not encountered this issue yet, but it cannot handle blanks in column A; by nature of using SUBTOTAL(3). I have no idea how that would be worked around.
As for this error, wrapping FILTERXML with IFERROR(,"") should suffice.
🤷‍♂️ I am a beginner and do not know how to write formulas. If possible, think of a solution for me. Thank you very much
The best formula I have found so far on the internet is yours.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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