VBA copying unique and distinct values Alphabetically

zubin

New Member
Joined
Sep 15, 2019
Messages
47
Hi All (My Gurus)
I have a code that separates Unique and distinct text values from a range("C25:C3000") and copies the new range to Column II2 of my worksheet....
The code works fine.......only change I would like is that the copied range should be in Alphabetic order ie A to Z
Please help
VBA Code:
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 = Range("C25:C" & lr)
For i = 1 To UBound(c, 1)
  d(c(i, 1)) = 1
Next i
Range("II2").Resize(d.Count) = Application.Transpose(d.keys)
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,​
so you must add some Range.Sort codeline after your last codeline or use an ArrayList - or SortedList - rather than a Dictionary …​
 
Upvote 0
An ArrayList beginner starter :​
VBA Code:
    With CreateObject("System.Collections.ArrayList")
        For Each V In Range("C25", Cells(Rows.Count, 3).End(xlUp)).Value2
            If Not .Contains(V) Then .Add V
        Next
           .Sort
            [II2].Resize(.Count).Value2 = Application.Transpose(.ToArray)
           .Clear
    End With
 
Upvote 0
An ArrayList beginner starter :​
VBA Code:
    With CreateObject("System.Collections.ArrayList")
        For Each V In Range("C25", Cells(Rows.Count, 3).End(xlUp)).Value2
            If Not .Contains(V) Then .Add V
        Next
           .Sort
            [II2].Resize(.Count).Value2 = Application.Transpose(.ToArray)
           .Clear
    End With

Sorry But...where do i put this code......Is it a replacement of the code i gave??
Will the copied range be in alphabetic order ?
Getting error on this code line
VBA Code:
 For Each V In Range("C25", Cells(Rows.Count, 3).End(xlUp)).Value2

Please let me know in detail......
 
Upvote 0
Ok Marc.....thanks a lot ...got it working.....
Code:
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 = Range("C25:C" & lr)
For i = 1 To UBound(c, 1)
  d(c(i, 1)) = 1
  Next i
Range("II2").Resize(d.Count) = Application.Transpose(d.keys)

Dim V As Variant
With CreateObject("System.Collections.ArrayList")
        For Each V In Range("C25", Cells(Rows.Count, 3).End(xlUp)).Value2
            If Not .Contains(V) Then .Add V
        Next
           .Sort
            [II2].Resize(.Count).Value2 = Application.Transpose(.ToArray)
           .Clear
    End With

Is it right ? Its working for me
Thanks for your attention.....
 
Upvote 0
As the Dictionary is useless so remove all before the Dim V codeline.​
You must notice it can be achieved directly with only Excel basics without the need of any external object​
like any Excel beginner operating manually and with the help of the Macro Recorder that's easy …​
 
Upvote 0
Ok Marc.....thanks a lot ...got it working.....
Code:
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 = Range("C25:C" & lr)
For i = 1 To UBound(c, 1)
  d(c(i, 1)) = 1
  Next i
Range("II2").Resize(d.Count) = Application.Transpose(d.keys)

Dim V As Variant
With CreateObject("System.Collections.ArrayList")
        For Each V In Range("C25", Cells(Rows.Count, 3).End(xlUp)).Value2
            If Not .Contains(V) Then .Add V
        Next
           .Sort
            [II2].Resize(.Count).Value2 = Application.Transpose(.ToArray)
           .Clear
    End With

Is it right ? Its working for me
Thanks for your attention.....
Sorry Marc...one observation, when the list in Range("C25:C3000") is refreshed ....ie either the range is deleted or new values put in, the copied range in II2 stays and is not refreshed or (deleted)
 
Upvote 0
As it was not explicit in the initial post - not the subject of this thread ! - I just can say it must be in an event of the worksheet module.​
For further help if necessary, create a new thread but with a complete code and the same for the explanation with nothin' to guess …​
 
Upvote 0
As it was not explicit in the initial post - not the subject of this thread ! - I just can say it must be in an event of the worksheet module.​
For further help if necessary, create a new thread but with a complete code and the same for the explanation with nothin' to guess …​
with a little guess work i worked my way around this shortcoming.......
Added this line just before your code
VBA Code:
Sheet1.Range("II2:II3000").ClearContents

Working fine.....Anyways Thanks a lot Marc
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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