Named Range of Unique values

gt213

Board Regular
Joined
Jul 5, 2016
Messages
61
I have an existing named range called 'MyRange' which is dynamic. I'd like to get a new named range using vba of all unique values in 'MyRange'. I'd prefer not to have the unique values listed anywhere in my spreadsheet.

Here is the code for the named range MyRange:

With Sort
k = Application.WorksheetFunction.CountA(ws1.Rows(1).EntireRow.Cells)
Set MyRange2 = ws1.Range(ws1.Cells(1, 2), ws1.Cells(1, k))
End With
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
This function can be used in VBA and inside a cell. It returns a non-contiguous range of cells that are unique to the set given by Rng.

Code:
Function GetRangeOfUnique(Rng As Range) As Range
  Dim u As Range
  Dim Cel As Range
  
  For Each Cel In Rng
    If Application.CountIf(Rng, Cel.Value) = 1 Then
      If Not u Is Nothing Then
        Set u = Union(u, Cel)
      Else
        Set u = Cel
      End If
      Debug.Print Cel.Address
    End If
    
  Next Cel
  If Not u Is Nothing Then Set GetRangeOfUnique = u
  
  
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,074
Messages
6,128,654
Members
449,462
Latest member
Chislobog

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