Copy Unique Values to New Sheet VBA

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
505
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm working on a macro that will take my selected range (only what I have selected and may or may not be a filtered range) and copy the values to a new sheet and remove duplicates. Here is my code so far, but it's not quite working. it copying things that are filtered out. Any help would be appreciated, thank you!

VBA Code:
Sub CopyUniqueValuesToNewSheet()
    Dim rng As Range
    Dim uniqueValues As Object
    Dim cell As Range
    Dim newSheet As Worksheet
    Dim newRow As Long
    
    ' Check if any range is selected
    If TypeName(Selection) <> "Range" Then
        MsgBox "No range selected!", vbExclamation
        Exit Sub
    End If
    
    ' Set the selected range
    Set rng = Selection
    
    ' Create a dictionary to store unique values
    Set uniqueValues = CreateObject("Scripting.Dictionary")
    
    ' Loop through each cell in the range and add unique values to the dictionary
    For Each cell In rng
        If Not uniqueValues.Exists(cell.value) Then
            uniqueValues.Add cell.value, Nothing
        End If
    Next cell
    
    ' Create a new worksheet
    Set newSheet = Worksheets.Add
    
    ' Initialize the row counter for the new sheet
    newRow = 1
    
    ' Copy unique values to the new sheet
    For Each Key In uniqueValues.keys
        newSheet.Cells(newRow, 1).value = Key
        newRow = newRow + 1
    Next Key
    
    ' Autofit columns on the new sheet
    newSheet.Columns.AutoFit
    
    ' Notify the user
    MsgBox "Unique values copied to new sheet successfully!", vbInformation
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Not test, but try:
VBA Code:
Sub CopyUniqueVisibleValuesToNewSheet()
    Dim rng As Range
    Dim visibleCells As Range
    Dim uniqueValues As Object
    Dim cell As Range
    Dim newSheet As Worksheet
    Dim newRow As Long
    
    If TypeName(Selection) <> "Range" Then
        MsgBox "No range selected!", vbExclamation
        Exit Sub
    End If
    
    Set rng = Selection
    
    On Error Resume Next
    Set visibleCells = rng.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    
    If visibleCells Is Nothing Then
        MsgBox "No visible cells in the selected range!", vbExclamation
        Exit Sub
    End If
    
    Set rng = visibleCells
    
    Set uniqueValues = CreateObject("Scripting.Dictionary")
    
    For Each cell In rng
        If Not uniqueValues.Exists(cell.Value) Then
            uniqueValues.Add cell.Value, Nothing
        End If
    Next cell
    
    Set newSheet = Worksheets.Add
    
    newRow = 1
    
    For Each Key In uniqueValues.Keys
        newSheet.Cells(newRow, 1).Value = Key
        newRow = newRow + 1
    Next Key
    
    newSheet.Columns.AutoFit
    
    MsgBox "Unique visible values copied to new sheet successfully!", vbInformation
End Sub
 
Upvote 0
Code:
Sub Or_Maybe_So()
Dim i As Long, c As Range
Dim unArr
Dim sh2 As Worksheet
Set sh2 = Worksheets("Sheet2")
    For Each c In Selection
        If c.EntireRow.Hidden = False Then unArr = unArr & "|" & c.Value
    Next c
unArr = Split(Mid(unArr, 2), "|")
    With sh2
        .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = unArr(0)
        For i = 1 To UBound(unArr)
            If WorksheetFunction.CountIf(.Range("A:A"), unArr(i)) = 0 Then .Cells(.Rows.Count, 1).End(xlUp).Offset(1).Value = unArr(i)     'Cells(lr + 1, 3).Value = frArr(j)
        Next i
    End With
End Sub
 
Upvote 0
Not test, but try:
VBA Code:
Sub CopyUniqueVisibleValuesToNewSheet()
    Dim rng As Range
    Dim visibleCells As Range
    Dim uniqueValues As Object
    Dim cell As Range
    Dim newSheet As Worksheet
    Dim newRow As Long
   
    If TypeName(Selection) <> "Range" Then
        MsgBox "No range selected!", vbExclamation
        Exit Sub
    End If
   
    Set rng = Selection
   
    On Error Resume Next
    Set visibleCells = rng.SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
   
    If visibleCells Is Nothing Then
        MsgBox "No visible cells in the selected range!", vbExclamation
        Exit Sub
    End If
   
    Set rng = visibleCells
   
    Set uniqueValues = CreateObject("Scripting.Dictionary")
   
    For Each cell In rng
        If Not uniqueValues.Exists(cell.Value) Then
            uniqueValues.Add cell.Value, Nothing
        End If
    Next cell
   
    Set newSheet = Worksheets.Add
   
    newRow = 1
   
    For Each Key In uniqueValues.Keys
        newSheet.Cells(newRow, 1).Value = Key
        newRow = newRow + 1
    Next Key
   
    newSheet.Columns.AutoFit
   
    MsgBox "Unique visible values copied to new sheet successfully!", vbInformation
End Sub
This worked! Thanks so much!
 
Upvote 0

Forum statistics

Threads
1,215,949
Messages
6,127,888
Members
449,411
Latest member
AppellatePerson

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