Sort & count

jayanthimurali

Board Regular
Joined
Oct 15, 2010
Messages
238
Can u please help

I have many names which i need to count manually and input and delete the names and sort the name whihc is highest

For example

arun
shela
robert
arun
rosy
arun
rosy

It should count arun as3 and rosy 2 shela 1 and robert 1,
only one name should apear as arun that is it should not be repetative like above but it should count as 3 and arun should be the first name as it pears 3 names than rosy, than robert and so on.

Please help if i copy the names from other file it should automatically sort as above

Thanks in advance
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:-
Results column "B".
Code:
[COLOR="Navy"]Sub[/COLOR] MG06Jul01
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] i           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] j           [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] temp1       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Temp2       [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Ray
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A1"), Range("A" & rows.count).End(xlUp))
    [COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
        .CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.value, 1
    [COLOR="Navy"]Else[/COLOR]
        .Item(Dn.value) = .Item(Dn.value) + 1
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
 
Ray = Application.Transpose(Array(.keys, .Items))
[COLOR="Navy"]For[/COLOR] i = 1 To UBound(Ray)
    [COLOR="Navy"]For[/COLOR] j = i To UBound(Ray)
        [COLOR="Navy"]If[/COLOR] Ray(j, 2) > Ray(i, 2) [COLOR="Navy"]Then[/COLOR]
            temp1 = Ray(i, 1)
            Temp2 = Ray(i, 2)
                Ray(i, 1) = Ray(j, 1)
                Ray(i, 2) = Ray(j, 2)
                    Ray(j, 1) = temp1
                    Ray(j, 2) = Temp2
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] j
[COLOR="Navy"]Next[/COLOR] i
Range("B1").Resize(.count) = Ray
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
There two vba codes to solve the problem.

Code:
Sub EasySol()
Dim LR As Long
 LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    Range("A1:A" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("B1"), Unique:=True
    
End Sub

Code:
Sub test()
Dim a, e, b(), n As Long
a = Range("a1", Range("a" & Rows.Count).End(xlUp)).Value
Columns("b").ClearContents
If Not IsArray(a) Then
     Range("b1").Value = a: Exit Sub
End If
ReDim b(1 To UBound(a, 1), 1 To 1)
With CreateObject("Scripting.Dictionary")
     .CompareMode = vbTextCompare
     For Each e In a
          If Not IsEmpty(e) And Not .exists(e) Then
               n = n + 1: b(n, 1) = e
               .Add e, Nothing
          End If
    Next
End With
Range("b1").Resize(n).Value = b
End Sub

EasySol is much easier for reading and following.

Biz
 
Upvote 0
Thank you very much

Biz I have tried the easysol

I have selected the and General and easysol at the top and input the code but iam getting runtime error 1004

Thanks
 
Upvote 0
Not sure why it does not work for you.
I have put comments so you can follow what's happening refer below.

Code:
Sub EasySol()
Dim LR As Long
'Defines Last row using Column A
 LR = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

'Advance copies unique values B1   
 Range("A1:A" & LR).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("B1"), Unique:=True
    
End Sub

Can you please paste your entire code?

Biz
 
Upvote 0
General easysol
Sub easySol()
Dim LR As Long
LR = ActiveSheet.Cells(Rows.Count, "A").End(x1up).Row
Range("A1:A" & LR).AdvancedFilter Action:=x1filtercopy, CopyToRange:=Range("B1"), Unique:=True
End Sub

I have return as u have given me
 
Upvote 0
Hi mate,

I think there is typo error have look bold text in both Before and After code.

Before
Code:
LR = ActiveSheet.Cells(Rows.Count, "A").End(x[B]1[/B]up).Row

Should be
Code:
 LR = ActiveSheet.Cells(Rows.Count, "A").End(x[B]l[/B]Up).Row


My suggestion is to copy and paste the code.

Biz
 
Upvote 0
You've got a "1" instead of "l" in two places, xlUp and xlfiltercopy. Have you changed both?

Also, if you've got more than around 40k rows (albeit unlikely) you may find that advanced filter doesn't always work very well.
 
Upvote 0

Forum statistics

Threads
1,224,569
Messages
6,179,603
Members
452,928
Latest member
VinceG

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