VBA code to display most frequent text

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
I have a range D2:D2500 with various names of clients. I am trying to figure out a VBA solution to display which one appears the most time.
On another thread other board contributors helped me with finding various array formula solution. ( http://www.mrexcel.com/forum/showthread.php?p=2641535#post2641535 )

But I am now looking for a VBA solution to display the result in MsgBox.

Any ideas?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This is sort of using suggestions in the link you provided - i.e. not a 'clean' VBA method, but try:
Code:
Sub ModalClientName ()
Dim Mode_Client as String
Range("A" & Rows.Count).FormulaArray = _
"=INDEX(D2:D2500,MODE(MATCH(D2:D2500,D2:D2500,0)))"
Mode_Client = Range("A" & Rows.Count)
Range("A" & Rows.Count).ClearContents
MsgBox "The client occurring the most is: " & Mode_Client
End Sub
Hopefully someone else can improve this without passing the result of the formula into a cell, but directly into the variable that can be called up with the msgbox
 
Upvote 0
I like your approach, incorporating the array formula, however, something isn't working. I get Range("A" & Rows.Count) = Error 2042 which doesn't mean anything to me.
 
Upvote 0
JackDanIce, thanks for your code. The problem was with the formula. I switched it out for =INDEX(D:D, 10000*MOD(MAX(COUNTIF(D1:D2545,D1:D2545)+(ROW(D1:D2545)/10000)),1), 1)
and it worked just fine.

Thanks.
 
Upvote 0
Is there another way to maybe incorporate the formula in the code directly like using the WorksheetFunction command?
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG10Mar45
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] oMax [COLOR=navy]As[/COLOR] Double
[COLOR=navy]Dim[/COLOR] K [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] Msg [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range(Range("D2"), Range("D" & 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]
oMax = Application.Max(Application.Transpose(.Items))
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] .keys
    [COLOR=navy]If[/COLOR] .Item(K) = oMax [COLOR=navy]Then[/COLOR]
        Msg = Msg & K & ","
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] K
MsgBox "The Number/s :- " & Msg & " Appeared " & oMax & " Times"
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks MickG, that works. It's a lot of code though. I liked the idea of using the array formula as the code seems shorter - if I could do it all in VBA and not paste it into the worksheet.
 
Upvote 0
The following code assigns the mode to a variable...

Code:
MyVar = Application.Evaluate("INDEX(D2:D2500,MODE(IF(D2:D2500<>"""",MATCH(D2:D2500,D2:D2500,0))))")
 
Upvote 0
Thanks everyone, for your help. I think I got now what I was looking for. MickG, I see the value in your code, as it is not limited to a set range, but goes until the end.
 
Upvote 0
For a dynamic range, try...

Code:
LastRow = Cells(Rows.Count, "D").End(xlUp).Row

MyVar = Application.Evaluate("INDEX(D2:D" & LastRow & ",MODE(IF(D2:D" & LastRow & "<>"""",MATCH(D2:D" & LastRow & ",D2:D" & LastRow & ",0))))")
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

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