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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,900
Office Version
  1. 365
Platform
  1. Windows
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

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
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

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
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

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
Is there another way to maybe incorporate the formula in the code directly like using the WorksheetFunction command?
 
Upvote 0

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
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

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
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

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,918
Office Version
  1. 365
Platform
  1. Windows
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

JPM

Active Member
Joined
Aug 1, 2002
Messages
407
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

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,918
Office Version
  1. 365
Platform
  1. Windows
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,191,587
Messages
5,987,515
Members
440,099
Latest member
wai2kit

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
Top