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

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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