Find the top five names and occurrence

viktiw

Board Regular
Joined
Jan 3, 2015
Messages
54
Hi Friends,
I have declared a range (A2:A25) in vba. Range contains names.
PFB the table.
Names
David
David
Adam
Mike
Adam
Adam
Adam
David
Harry
Robert
Robert
John
John
John
John
Adam
Mike
Mike
John
John
Robert
Robert
Tom
Tom

<tbody>
</tbody>

<tbody>
</tbody>
Now i would like to display a message box with name top five occurrence with their occurrence.
Like this
John 6
Adam 5
Robert 4
David 3
Mike 3


Let me know your comments, any help is appreciated.
Please make sure code is simpler and easily understandable as I am new to vba.

Thanks
viktiw
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
with a pivot table you should be able to get it done

Excel 2010
ABCD
1NamesRow LabelsCount of Names
2DavidJohn6
3DavidAdam5
4AdamRobert4
5MikeDavid3
6AdamMike3
7AdamGrand Total21
8Adam
9David
10Harry
11Robert
12Robert
13John
14John
15John
16John
17Adam
18Mike
19Mike
20John
21John
22Robert
23Robert
24Tom
25Tom
Sheet5

Just a regular pivot table and then a filter to show the Top 5 values and that should give you want you want

If your original data is formatted as a TABLE, it will update as new data is added and so will the pivot table and so will your Top 5 values
 
Upvote 0
Hi momentman,
thanks, but I am looking for a macro that will display a message box with these values.
Can we do that ?

viktiw
 
Upvote 0
viktiw,

Welcome to the MrExcel forum.

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


Here is a macro for your consideration.

Sample raw data:


Excel 2007
A
1Names
2David
3David
4Adam
5Mike
6Adam
7Adam
8Adam
9David
10Harry
11Robert
12Robert
13John
14John
15John
16John
17Adam
18Mike
19Mike
20John
21John
22Robert
23Robert
24Tom
25Tom
26
Sheet1


After the macro in a MessageBox:

John 6
Adam 5
Robert 4
David 3
Mike 3



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub GetTopFive()
' hiker95, 01/03/2015, ME826885
Dim c As Range, lc As Long, r As Long, lr As Long, t As String
Application.ScreenUpdating = False
lc = Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In Range("A2:A25")
    If Not .Exists(c.Value) Then
      .Add c.Value, 1
    Else
      .Item(c.Value) = .Item(c.Value) + 1
    End If
  Next c
  Cells(1, lc + 2).Resize(.Count, 2) = Application.Transpose(Array(.Keys, .Items))
End With
lr = Cells(Rows.Count, lc + 2).End(xlUp).Row
Range(Cells(1, lc + 2), Cells(lr, lc + 3)).Sort key1:=Cells(1, lc + 3), order1:=2, key2:=Cells(1, lc + 2), order1:=1
If lr > 5 Then
  For r = 1 To 5
    t = t & Cells(r, lc + 2) & " " & Cells(r, lc + 3) & vbLf
  Next r
Else
  For r = 1 To lr
    t = t & Cells(r, lc + 2) & " " & Cells(r, lc + 3) & vbLf
  Next r
End If
Range(Cells(1, lc + 2), Cells(lr, lc + 3)).ClearContents
Application.ScreenUpdating = True
MsgBox (t)
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetTopFive macro.
 
Upvote 0
hi hiker95,
Yes it is working...
But i am not able to understand how this code works.
Is there any simpler code, considering I am new to vba ??

viktiw
 
Upvote 0
Pl see this code. you have to enter the input range.
Code:
Sub ReadNamesAndNumbers()
Dim Iprng As Range, cel As Range
Dim temp As String, disp As String


Set Iprng = Range(InputBox("Enter the range (Eg A1:A23):"))


For Each cel In Iprng


    If InStr(1, temp, cel.Value) = 0 Then
    disp = disp & Chr(10) & cel.Value & WorksheetFunction.Rept(" ", 20 - Len(cel.Value)) & WorksheetFunction.CountIf(Iprng, cel.Value)
    temp = temp & cel.Value
    End If


Next cel


If temp <> "" Then MsgBox (disp)


End Sub
 
Upvote 0
Sorry previous code gives entire list. this code gives top 5 only
Code:
Sub ReadNamesAndNumbers()
Dim Iprng As Range, cel As Range
Dim temp As String, disp As String, tempdisp  As String
Dim Namecount(1 To 5) As Long


Set Iprng = Range(InputBox("Enter the range (Eg A1:A23):"))


For T = 1 To 5


For Each cel In Iprng


    If InStr(1, temp, cel.Value) = 0 Then
        If Namecount(T) < WorksheetFunction.CountIf(Iprng, cel.Value) Then
        Namecount(T) = WorksheetFunction.CountIf(Iprng, cel.Value)
        tempdisp = cel.Value & WorksheetFunction.Rept(" ", 20 - Len(cel.Value)) & Namecount(T)
        tempcelval = cel.Value
        End If
    End If


Next cel
temp = tempcelval & temp
tempcelval = ""
disp = disp & Chr(10) & tempdisp


Next T


If temp <> "" Then MsgBox (disp)


End Sub
 
Upvote 0
Now i would like to display a message box with name top five occurrence with their occurrence.
Like this
John 6
Adam 5
Robert 4
David 3
Mike 3
Welcome to the MrExcel board!

Can you confirm what should happen if there are ties for 5th place? For example if your data had another "Tom" where you have "Harry".
 
Upvote 0
Hell all!!
I like this trick but I preffered with the Formula because Update instantly
is a way to do it with formula
Thank you all
I' Learning a lot from you
 
Upvote 0
Sorry
I wanted say Hello All my Typo
 
Upvote 0

Forum statistics

Threads
1,214,897
Messages
6,122,148
Members
449,066
Latest member
Andyg666

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