List unique values from a named range

Andrew_K99

New Member
Joined
Dec 4, 2007
Messages
26
I am trying to find a way to create a list of unique items from a named range. I have found a few solutions using filters for ranges that are contained in the one column but not named ranges that contain multiple rows and multiple columns.

Is there any way to copy all of the unique items from the named range to another location? Lets call the named range Table1 and it spans from B2 to J100.

Thanks in advance!!
Andrew
 
Assuming column L (column 12) is still available and not occupied by those other tables, then yes, it's simple with one run through the macro by substituting this codeline in what I first posted:
For Each cell In Range("Table1")

with this:
For Each cell In Range("Table1, Table2")

If you add more named ranges, just follow that syntax, example
For Each cell In Range("Table1, Table2, Table3")
Thanks again! I was close, I tried changing it to Range("Table1", "Table2", "Table3") but it errored out.

It works great but when there are blank cells in the named ranges it puts multiple blank cells in the unique list. This isn't the end of the word but would be nice if it weren't there.

Thanks again!!!!!
Andrew
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I don't know if you've tried it but the code I supplied ignored blank or null values found.
 
Upvote 0
It works great but when there are blank cells in the named ranges it puts multiple blank cells in the unique list. This isn't the end of the word but would be nice if it weren't there.
This checks if the cell is displaying a value and then acts on it; otherwise goes to the next cell, essentially skipping blank cells in the range(s) which sounds like what you are asking for.

Code:
Sub Test2() 
Application.ScreenUpdating = False 
Dim cell As Range, x&, varCell As Variant 
x = 2 
Columns(12).Clear 
Range("L1").Value = "Unique entries in Table1" 
For Each cell In Range("Table1, Table2") 
If Len(cell.value) > 0 Then
varCell = Application.Match(cell.Value, Columns(12), 0) 
If IsError(varCell) Then 
Err.Clear 
Cells(x, 12).Value = cell.Value 
x = x + 1 
End If
End If
Next cell 
Range("L1").CurrentRegion.Sort Key1:=Range("L2"), Order1:=xlAscending, Header:=xlYes 
Application.ScreenUpdating = True 
End Sub
 
Upvote 0
Thanks Tom it works great!! This'll save a lot of time!

Zack ... I haven't tried your could yet but I will.

Thanks everyone for their input!!!
 
Upvote 0
Okay ... one last thing ...

Is there anyway to exclude certain values, say "Level", "Height", etc.?
- or -
Is there anyway to check to see if the named range exists before seaching through it? (as this errors out)

I'll try to explain what I have ...
I have 10 named ranges, which are each part of a table. Each table has headers and other misc. calculations below them. Occationally multiple tables are deleted, so this deletes the named range. My idea was to have one large named range that covers all the other named ranges so if rows are deleted the primary named range still exists. The problem with this is the headers, titles etc. will get counted.

Your help is GREATLY appreciated!!!
Andrew
 
Upvote 0
A friendly tip, if you describe your entire project at the start, instead of these bit-by-bit requests along the way, it will take less time and effort to get a final resolution because the suggested programming approaches usually depend on the big picture. In the future just lay out the entire circumstances involved to help you and the people helping you.

Now then, with numerous named ranges, some of which might or might not actually be bona fide named ranges on the sheet you are trying to extract unique cell values from, do this:

Insert a new worksheet and name it "ZZZ" (without the quotes).

In new sheet ZZZ in column A starting in cell A1, list the all the named ranges you might ever have established on the worksheet of interest. Example, in sheet ZZZ, cell A1 contains Table1, cell A2 contains Table2, cell A3 contains Table3, etc. Do this even though, maybe, Table2 is currently not an active named range on this main worksheet you are working with.

Do not skip rows when you list those potential names on sheet ZZZ.

If you feel like it, go ahead and hide sheet ZZZ when you are done listing all potential named ranges.

Regarding the header values of all those named ranges which you do NOT want listed among the unique cell values, I assume those headers occupy the first row of their respective named ranges. Example, if range name Table2 occupies range B6:J10, then B6:J6 should include the field headers for named range Table2. If this is not the case, then make it be the case, which is good design practice for tables and databases.

Recall, the macro I first posted was based on the inference that column J was the last (rightmost) column where named ranges are involved, meaning that columns K, L, and M were empty, further meaning that column L was not touching any data or any named range addresses. If that is still the case, the below macro will do what you want, which is, only list in column L the unique values below the first header row from all recognized named ranges. Then the list in column L will be sorted alphabetically and autofitted for width.


Code:
Sub Test3()
Application.ScreenUpdating = False
Dim RangeCell As Range, cell As Range
Dim x&, varCell As Variant
x = 2: Columns(12).Clear: Err.Clear
Range("L1").Value = "Unique entries in Tables"

For Each RangeCell In Sheets("ZZZ").Range("A1").CurrentRegion
On Error Resume Next
If Not ActiveWorkbook.Names(RangeCell.Value) Is Nothing Then
If Err.Number <> 0 Then
Err.Clear
Else

With Range(RangeCell.Value)
For Each cell In .Offset(1).Resize(.Rows.Count - 1)
If Len(cell.Value) > 0 Then
varCell = Application.Match(cell.Value, Columns(12), 0)
If IsError(varCell) Then
Err.Clear
Cells(x, 12).Value = cell.Value
x = x + 1
End If
End If
Next cell
End With

End If
End If

Next RangeCell

Range("L1").CurrentRegion.Sort Key1:=Range("L2"), Order1:=xlAscending, Header:=xlYes
Columns(12).AutoFit
Application.ScreenUpdating = True
End Sub
 
Upvote 0
THANKS again Tom, you've been a great help!!!

In the future I will explain what I wanting to achieve in my original post. I was hoping I could figure it out but I am not quite there (obviously!).

Andrew
 
Upvote 0
schielrn ... that code works beautifully. Can't say I understand why it works but am just happy that it does.
 
Upvote 0

Forum statistics

Threads
1,215,891
Messages
6,127,602
Members
449,388
Latest member
macca_18380

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