Filter Unique Values and Sort A to Z then Paste the Result

ExcelMentee

Banned - Rules violations
Joined
Jan 11, 2021
Messages
31
Office Version
  1. 365
Platform
  1. Windows
I have been using below code to Filter the Unique values from Sheet1 and paste them into Sheet2 my code is working fine. But it has one issue that is when i remove any value from Sheet1.Range(C4:C) cell it gives empty cell in Sheet2 like in below image.

I want that if i remove any cell value from Sheet1 range then Code should automatically adjust it. there should not be any empty cell in Sheet2 Range.

I also wants to add sort function in the code so unique values will be popup with sorting A to Z in sheet2.

I tried at my end to do both things but cannot do. Your help in this regard will be highly appreciated.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim d As Object, c As Variant, i As Long, lr As Long
Set d = CreateObject("Scripting.Dictionary")
lr = Cells(Rows.Count, 1).End(xlUp).Row
c = Sheet1.Range("C4:C" & lr)
For i = 1 To UBound(c, 1)
  d(c(i, 1)) = 1
Next i
Sheet2.Range("C4").Resize(d.Count) = Application.Transpose(d.keys)
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
As you are using 365, why not just use a simple formula
Excel Formula:
=SORT(UNIQUE(FILTER(Sheet1!C2:C10000,Sheet1!C2:C10000<>"")))
 
Upvote 0
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim d As Object, c As Variant, i As Long, lr As Long
    Dim lst As Object
    Set d = CreateObject("Scripting.Dictionary")
    Set lst = CreateObject("System.Collections.Sortedlist")
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    c = Sheets(1).Range("C4:C" & lr)
    For i = 1 To UBound(c, 1)
        If c(i, 1) <> "" Then d(c(i, 1)) = 1
    Next i
    myUniqueList = Application.Transpose(d.keys)
    With lst
        For i = 1 To UBound(myUniqueList)
            .Add myUniqueList(i, 1), i
        Next
        For i = 0 To .Count - 1
              Sheets(2).Range("C" & i + 4) = .GetKey(i)
        Next
    End With
End Sub
 
Upvote 0
In that case how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim Cl As Range

   If Not Intersect(Target, Range("C:C")) Is Nothing Then
      With CreateObject("system.collections.arraylist")
         For Each Cl In Range("C4", Range("C" & Rows.Count).End(xlUp))
            If Cl.Value <> "" Then
               If Not .contains(Cl.Value) Then .Add Cl.Value
            End If
         Next Cl
         .Sort
         Sheet2.Range("C4").Resize(.Count).Value = Application.Transpose(.toarray)
      End With
   End If
End Sub
 
Upvote 0
Solution

Fluff

Thank you so much. I have been stunned by looking at your code that how short it is and working perfectly

mart37

Thank you for the answer. Bu your code just loads these two values instead of all from Sheet1 Col "C"

1611672834814.png
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,409
Members
448,959
Latest member
camelliaCase

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