Finding the most occurrences of a value in a data set, using VBA.

Technology

New Member
Joined
Apr 29, 2021
Messages
25
Office Version
  1. 365
Platform
  1. Windows
With this data starting at A1, how would we go about finding which column has the most occurrences of 0's. Using VBA function, place the result in A25.

738636
169425
51010462
176706
156397
178816
280602
7510130
859940
643242
847669
641431
230884
035476
710083
418396
4111035
730414
366961
823668
0011012
529437
573816
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
What if multiple columns have the same maximum number of zeros? Is that possible and if so, how should the result(s) be presented?
 
Upvote 0
Result for each column can be presented even its repeated. The desired result is the column with the most 0's. I believe that column C with 3, 0's.
 
Upvote 0
Try this. Select range and run macro
VBA Code:
Sub FindMostFreq()

Dim MaxCount As Long, nCount As Long
Dim MaxVal As Variant, xVal As Variant
Dim cell As Range, rngNum As Range
Dim dict As Object

Set dict = CreateObject("Scripting.Dictionary")
Set rngNum = Selection

MaxCount = 0
MaxVal = ""
For Each cell In rngNum
    If Not cell.Value2 = "" Then
        xVal = cell.Value2
        dict(xVal) = dict(xVal) + 1
        nCount = dict(xVal)
        If nCount > MaxCount Then
            MaxCount = nCount
            MaxVal = xVal
        End If
    End If
Next
MsgBox "Most occurance : " & MaxVal & " occurs " & MaxCount & " times"

End Sub

opps. I did not read your requirement properly :confused:
 
Upvote 0
Result for each column can be presented even its repeated. The desired result is the column with the most 0's. I believe that column C with 3, 0's.
Column C actually has 4 0's. However, your answer about multiples is still not clear. Suppose columns A, C and F all had four 0's.
Should cell A25 contain "A, C, F"
or A25 holds "A", B25 holds "C" and C25 holds "F"
or something else?
 
Upvote 0
@Peter_SSs
Ok, I get what your say. My bad on the counting, as well. A25 holds "A", B25 holds "C" and C25 holds "F" would work perfect.
 
Upvote 0
A25 holds "A", B25 holds "C" and C25 holds "F" would work perfect.
Give this a try with a copy of your workbook.

VBA Code:
Sub Max_Zeros()
  Dim maxVal As Long, c As Long
  Dim s As String
  Dim cols As Variant
  
  With Range("A25:F25")
    .Formula = "=COUNTIF(A1:A23,0)"
    maxVal = Application.Max(.Value)
    For c = 1 To .Columns.Count
      If .Cells(c).Value = maxVal Then s = s & " " & Chr(64 + c)
    Next c
    .ClearContents
    cols = Split(Mid(s, 2))
    .Resize(, UBound(cols) + 1).Value = cols
  End With
End Sub
 
Upvote 0
Solution
Give this a try with a copy of your workbook.

VBA Code:
Sub Max_Zeros()
  Dim maxVal As Long, c As Long
  Dim s As String
  Dim cols As Variant
 
  With Range("A25:F25")
    .Formula = "=COUNTIF(A1:A23,0)"
    maxVal = Application.Max(.Value)
    For c = 1 To .Columns.Count
      If .Cells(c).Value = maxVal Then s = s & " " & Chr(64 + c)
    Next c
    .ClearContents
    cols = Split(Mid(s, 2))
    .Resize(, UBound(cols) + 1).Value = cols
  End With
End Sub
The following only states the column's in row 25, instead of which column has the most 0's.
 
Upvote 0
Here is may sample data (I changed a few of the values so multiple columns had the max number of 0's [4]) and the results of the code.
I have just highlighted the 0 values to make checking easier.
Is this the result you would want for this data?

Technology.xlsm
ABCDEF
1738636
2169425
351010462
4176706
5156397
6178816
7280602
80510130
9059940
10643242
11847669
12641431
13230880
14035470
15710083
16418396
174111035
18730414
19366961
20823668
210011012
22529437
23573816
24
25ACF
Sheet1
 
Upvote 0
Here is may sample data (I changed a few of the values so multiple columns had the max number of 0's [4]) and the results of the code.
I have just highlighted the 0 values to make checking easier.
Is this the result you would want for this data?

Technology.xlsm
ABCDEF
1738636
2169425
351010462
4176706
5156397
6178816
7280602
80510130
9059940
10643242
11847669
12641431
13230880
14035470
15710083
16418396
174111035
18730414
19366961
20823668
210011012
22529437
23573816
24
25ACF
Sheet1
Thank you, I would prefer that the code stated that just C based on the data had the most zeros compared to the other columns. Maybe having a count of each column then the code selecting the highest value. I'm not sure the best way of solving this one but I just want to say that I really appreciate your help. I'm definitely learning.
 
Upvote 0

Forum statistics

Threads
1,215,604
Messages
6,125,792
Members
449,260
Latest member
Mrw1

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