VBA filter and concatenate

Dannybert

New Member
Joined
Aug 22, 2021
Messages
11
Office Version
  1. 2019
Platform
  1. Windows
I have a workbook where I have 3 tables stacked on Sheet1.

So for example: The first table starts from A2 and ends on A10, the second from A13 to A19 etc.

I want to extract the first column values purely on unique values and without the headers or blank cells. So it should give me a nice column on Sheet2 with only the unique values without any zeros or blanks.

EXAMPLE:
oranges
apples
bananas
pineapples
grapes

Also, I want the filtered values on Sheet2 to be concatenated into another cell with a comma in between the values (except for the last value.

EXAMPLE:
oranges,apples,bananas,pineapples,grapes

I want to use VBA for it so it works in multiple versions of Excel.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
I mean to filter the blanks and headers and be left with unique values, so I don’t want to filter the unique values only! It should display every value but only once like the unique function does in 365.
 
Upvote 0
Try running this code

VBA Code:
Sub jvr()
  With CreateObject("scripting.dictionary")
     For Each tbl In Sheets(1).ListObjects
        ar = tbl.DataBodyRange
        For i = 1 To UBound(ar)
          If ar(i, 1) <> "" Then c00 = .Item(ar(i, 1))
        Next
     Next
    Sheets(2).Cells(1).Resize(.Count) = Application.Transpose(.keys)
    Sheets(2).Cells(1, 4) = Join(.keys, ", ")
  End With
End Sub
 
Upvote 0
Solution
Try running this code

VBA Code:
Sub jvr()
  With CreateObject("scripting.dictionary")
     For Each tbl In Sheets(1).ListObjects
        ar = tbl.DataBodyRange
        For i = 1 To UBound(ar)
          If ar(i, 1) <> "" Then c00 = .Item(ar(i, 1))
        Next
     Next
    Sheets(2).Cells(1).Resize(.Count) = Application.Transpose(.keys)
    Sheets(2).Cells(1, 4) = Join(.keys, ", ")
  End With
End Sub
Hello JEC,

This worked like a charm! Thanks a lot!!!
 
Upvote 0
Here is another possibility with less looping.

VBA Code:
Sub MakeList()
  Dim tbl As ListObject
  
  For Each tbl In Sheets(1).ListObjects
    Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(tbl.Range.Rows.Count - 1).Value = tbl.DataBodyRange.Value
  Next tbl
  With Sheets(2).Columns("A")
    .SpecialCells(xlBlanks).Delete Shift:=xlUp
    .RemoveDuplicates Columns:=1, Header:=xlNo
    .Cells(1, 4).Value = Join(Application.Transpose(.SpecialCells(xlConstants).Value), ", ")
  End With
End Sub
 
Upvote 0
Here is another possibility with less looping.

VBA Code:
Sub MakeList()
  Dim tbl As ListObject
 
  For Each tbl In Sheets(1).ListObjects
    Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(tbl.Range.Rows.Count - 1).Value = tbl.DataBodyRange.Value
  Next tbl
  With Sheets(2).Columns("A")
    .SpecialCells(xlBlanks).Delete Shift:=xlUp
    .RemoveDuplicates Columns:=1, Header:=xlNo
    .Cells(1, 4).Value = Join(Application.Transpose(.SpecialCells(xlConstants).Value), ", ")
  End With
End Sub
Thanks for helping, but the other code is working perfectly so I won't need this one now, but I might use it when changing some stuff to improve it ;)
 
Upvote 0
it does have less looping, but is has more pasting. On big data sets, the dictionary is probably still faster. In this case you never notice the difference.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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