Counting Multiple instances

Frostrunr

New Member
Joined
Sep 15, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I'm trying to figure out the code for counting multiple instances of different text in a column and displaying it all under one msgbox as a summary.

So far I have only been able to get one count going with the below code;

Sub Count_Instances()
Dim instances As Long

instances = WorksheetFunction.CountIf(Columns("A"), "RED")

If instances <> 0 Then _
MsgBox "Found " & instances & " RED", vbInformation, "RED"
End Sub

However I want it to check for 6 different colors count how many instances there are of each and display them as a summary on one message box.
i.e
7 x Red
4 x Green
3 x Blue
1 x Yellow
4 x Pink
2 x Black

Sorry if its a massive ask :D I've been puzzling my way trying to figure it out all week, starting to think its near impossible. As I'm sure everyone does when trying to figure out VBA Code :D

Any Guidance or ideas would be greatly appreciated.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,879
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Maybe something like this...

VBA Code:
Sub aTest()
    Dim arrColors As Variant, instances As Long
    Dim msg As String, i As Long
    
    arrColors = Array("Red", "Green", "Blue", "Yellow", "Pink", "Black")
    For i = LBound(arrColors) To UBound(arrColors)
        instances = Application.CountIf(Columns("A"), arrColors(i))
        If instances > 0 Then msg = msg & instances & " x " & arrColors(i) & vbNewLine
    Next i
    MsgBox msg, Title:="Colors Count"
End Sub

Hope this helps

M.
 
Solution

Frostrunr

New Member
Joined
Sep 15, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
that worked perfectly thank you so much :biggrin: !!!!!!!!!
 

Forum statistics

Threads
1,144,341
Messages
5,723,808
Members
422,518
Latest member
quack_quack

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