macro_learning
New Member
Hi,
I have found the below code to get the unique values/count in any column,
Sub UniqueList()
Application.ScreenUpdating = False
Dim lastRow As Long
Dim i As Long
Dim dictionary As Object
Set dictionary = CreateObject("scripting.dictionary")
Sheet1.Activate
lastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For i = 1 To lastRow
If Len(Cells(i, "A")) <> 0 Then
dictionary.Add Cells(i, "A").Value, 1
End If
Next
Sheets(1).Range("C1") = dictionary.Count
Sheets(1).Range("B1").Resize(dictionary.Count).Value = _
Application.Transpose(dictionary.keys)
Application.ScreenUpdating = True
MsgBox dictionary.Count & " unique cell(s) were found and copied."
End Sub
it is executing below code properly:
MsgBox dictionary.Count & " unique cell(s) were found and copied."
However below code is not working , I want this to paste unique count and values in column c and b respectively,
Sheets(1).Range("C1") = dictionary.Count
Sheets(1).Range("B1").Resize(dictionary.Count).Value = _
Application.Transpose(dictionary.keys)
And if possible can anyone please explain me how this vba UniqueList() is actually working and giving the desired output.
Regards,
macro_learning
I have found the below code to get the unique values/count in any column,
Sub UniqueList()
Application.ScreenUpdating = False
Dim lastRow As Long
Dim i As Long
Dim dictionary As Object
Set dictionary = CreateObject("scripting.dictionary")
Sheet1.Activate
lastRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For i = 1 To lastRow
If Len(Cells(i, "A")) <> 0 Then
dictionary.Add Cells(i, "A").Value, 1
End If
Next
Sheets(1).Range("C1") = dictionary.Count
Sheets(1).Range("B1").Resize(dictionary.Count).Value = _
Application.Transpose(dictionary.keys)
Application.ScreenUpdating = True
MsgBox dictionary.Count & " unique cell(s) were found and copied."
End Sub
it is executing below code properly:
MsgBox dictionary.Count & " unique cell(s) were found and copied."
However below code is not working , I want this to paste unique count and values in column c and b respectively,
Sheets(1).Range("C1") = dictionary.Count
Sheets(1).Range("B1").Resize(dictionary.Count).Value = _
Application.Transpose(dictionary.keys)
And if possible can anyone please explain me how this vba UniqueList() is actually working and giving the desired output.
Regards,
macro_learning