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
 
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
Why, when I run the following VBA code in another sheet, the value of the formula cell shows a !value# error.

VBA code:
Option Explicit


Sub Filtering_Country()
'
' Filtering Macro
'
Dim Country As String

Country = Worksheets("Keywords Analysis").Range("D1").Value

With Worksheets("Projects")
With .Range("AF2:AF" & .Cells(.Rows.Count, "R").End(xlUp).Row)
.AutoFilter 'Turn off any previous filtering
.AutoFilter Field:=32, Criteria1:=Country
End With
.AutoFilterMode = False
End With
End Sub

Formula in C2 in another sheet:
=SORT(TRIM(ItemList(Projects!AI2:AI2100,FALSE)))
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When posting vba code in the forum, please use the available vba code tags, not the Quote tags. My signature block below has more details.

1694948674669.png


When posting a worksheet formula in the forum, please use the Formula tags, not the Quote tags

1694948762016.png


In your vba code you are filtering for Country but then immediately removing that filter. What is the point of that?

1694948599685.png



What row are your headings in on sheet 'Projects'?
 
Upvote 0
When posting vba code in the forum, please use the available vba code tags, not the Quote tags. My signature block below has more details.

View attachment 98853

When posting a worksheet formula in the forum, please use the Formula tags, not the Quote tags

View attachment 98854

In your vba code you are filtering for Country but then immediately removing that filter. What is the point of that?

View attachment 98852


What row are your headings in on sheet 'Projects'?
They are in the first row.
 
Upvote 0
They are in the first row.
Thanks. With that arrangement, the code you posted in post #51 is not working for me.

What is the last column with a heading in row 1?

You also did not address this:
In your vba code you are filtering for Country but then immediately removing that filter. What is the point of that?
 
Upvote 0
Thanks. With that arrangement, the code you posted in post #51 is not working for me.

What is the last column with a heading in row 1?

You also did not address this:
1. The last column is AQ, its title is in Persian: کل کلمات کلیدی
2. In your vba code you are filtering for Country but then immediately removing that filter. What is the point of that?
I got the code from the internet and I don't know the details unfortunately.
 
Upvote 0
Thanks. With that arrangement, the code you posted in post #51 is not working for me.

What is the last column with a heading in row 1?

You also did not address this:
I want to filter the AF column in the projects sheet based on a value in another sheet (Keywords Analysis) in cell D1 that can be changed.
When the filter operation is done. The values in the third sheet (keywords) that have following formula show the value error.
=SORT(TRIM(ItemList(Projects!AI2:AI2100,FALSE)))
 
Last edited:
Upvote 0
1. The last column is AQ, its title is in Persian: کل کلمات کلیدی
2. In your vba code you are filtering for Country but then immediately removing that filter. What is the point of that?
I got the code from the internet and I don't know the details unfortunately.
Ok, give this a try instead of the previous filtering code with a copy of your workbook.

VBA Code:
Sub Filtering_Country_v2()
  '
  ' Filtering Macro
  '
  Dim Country As String
  
  Country = Worksheets("Keywords Analysis").Range("D1").Value
  Application.Calculation = xlCalculationManual
  With Worksheets("Projects")
    .AutoFilterMode = False
    .Range("A1:AQ" & .Cells(.Rows.Count, "AF").End(xlUp).Row).AutoFilter Field:=32, Criteria1:=Country
  End With
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0
Ok, give this a try instead of the previous filtering code with a copy of your workbook.

VBA Code:
Sub Filtering_Country_v2()
  '
  ' Filtering Macro
  '
  Dim Country As String
 
  Country = Worksheets("Keywords Analysis").Range("D1").Value
  Application.Calculation = xlCalculationManual
  With Worksheets("Projects")
    .AutoFilterMode = False
    .Range("A1:AQ" & .Cells(.Rows.Count, "AF").End(xlUp).Row).AutoFilter Field:=32, Criteria1:=Country
  End With
  Application.Calculation = xlCalculationAutomatic
End Sub
It worked Thank you very very very much
 
Upvote 0
Ok, give this a try instead of the previous filtering code with a copy of your workbook.

VBA Code:
Sub Filtering_Country_v2()
  '
  ' Filtering Macro
  '
  Dim Country As String
 
  Country = Worksheets("Keywords Analysis").Range("D1").Value
  Application.Calculation = xlCalculationManual
  With Worksheets("Projects")
    .AutoFilterMode = False
    .Range("A1:AQ" & .Cells(.Rows.Count, "AF").End(xlUp).Row).AutoFilter Field:=32, Criteria1:=Country
  End With
  Application.Calculation = xlCalculationAutomatic
End Sub
Is it possible to write the VBA code as a UDF to automatically perform filtering by changing the value of cell D1?
Of course, if cell D1 is empty, filtering is not done. In other words, when cell D1 is empty, do not filter for empty cells.
 
Last edited:
Upvote 0
It worked Thank you very very very much

Ok, give this a try instead of the previous filtering code with a copy of your workbook.

VBA Code:
Sub Filtering_Country_v2()
  '
  ' Filtering Macro
  '
  Dim Country As String
 
  Country = Worksheets("Keywords Analysis").Range("D1").Value
  Application.Calculation = xlCalculationManual
  With Worksheets("Projects")
    .AutoFilterMode = False
    .Range("A1:AQ" & .Cells(.Rows.Count, "AF").End(xlUp).Row).AutoFilter Field:=32, Criteria1:=Country
  End With
  Application.Calculation = xlCalculationAutomatic
End Sub
Is it possible to write the code in such a way that when the value of cell D1 is changed, the VBA code is automatically executed and the same sheet is refreshed.
Please accept my sincere thanks for taking the time for me.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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