Get the 2nd, 3rd more frecuent values in a filtered table with more than 1 numeric column

drom

Well-known Member
Joined
Mar 20, 2005
Messages
528
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and Thanks in advance!

I have a table with:
  • Max 9 columns ( for this eg: lets say 6 columns A:F)
  • Unknown rows (for this eg: 23 rows)
    • So My Table's databodyrange is A9:F30
    • My Table's Header is A8:F8
  • B9:F30 are cells with numbers, not blank cells, not text cells, numeric values, for this eg, I am using the formula, =RANDBETWEEN(1,100)

I know how to get the 1st, 2nd, 3rd, 4rd and 5th more frecuent values in this table using VBA
When this table is filtered by Column A, by country, and/or when is not filtered (Easy with VBA)


To get the more frecuent number I can use:
  • =Mode(B9:F30)
  • =MODE(Table3[[VALUES1]:[VALUES5]])

But I would like a formula:
  1. To get the more frecuent number in my table when the table is filtered by column A
  2. To get the 2nd more frecuent number whenThe table is not filtered
  3. To get the 2nd more frecuent number when The table is filtered
I guess If I get the answer for my 3 questions, I will be able to get the 3rd, 4rd, and 5th more frecuent numbers in this table when filtered or not filtered

I am attaching my WorkSheet:, this Worksheet is done just for this eg:

Book1
ABCDEFG
1
2This table can be filtered by Country
3
41st More Frecuent Number:??
52nd More Frecuent Number:??
63rd More Frecuent Number:??
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5
9Spain7761752459
10France9618601397
11UK894375153
12USA984221994
13Spain1861704772
14USA7625446591
15Portugal6938442122
16Italy97437853
17Belgium2421113142
18Mexico7944263348
19Mexico2547588164
20uk277145412
21Italy7454351773
22Usa7660604277
23USA537567055
24UK9651881152
25Italy5864323964
26Spain5925216248
27Spain7940341347
28France4784853717
29Portugal628906648
30France4994708982
31
Sheet2
Cell Formulas
RangeFormula
B9:F30B9=RANDBETWEEN(1,100)
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Map1
ABCDEFG
41st More Frecuent Number:7272
52nd More Frecuent Number:2121
63rd More Frecuent Number:6329
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5visible
9Spain9293480641
10France26194140861
12USA75721243141
13Spain48332415551
14USA15187631461
15Portugal36455188121
16Italy41268779231
21Italy4556670361
22Usa68632372731
23USA85466357871
25Italy132172100651
26Spain467810027451
27Spain58394632921
28France12333369891
29Portugal86446015861
30France26528440221
31
32
33
34
35
36
37
38
39
40725725
41215215
42633292
43673552
44152632
45292962
Blad1
Cell Formulas
RangeFormula
D4:D6D4=+B40
E4:E6E4=+E40
B9:F10,B12:F16,B21:F23,B25:F30B9=RANDBETWEEN(1,100)
G9:G10,G12:G16,G21:G23,G25:G30G9=(SUBTOTAL(102,B9:F9)>0)+0


VBA Code:
Sub counting()
     Dim bVis, Res()
     With Sheets("blad1")
          a = Range("A8").CurrentRegion.Resize(, 7)
          ReDim Res(1 To UBound(a) * UBound(a, 2), 1 To 5)
          For i = 2 To UBound(a)
               bVis = a(i, 7) <> 0
               For j = 2 To UBound(a, 2) - 1
                    r = Application.Match(a(i, j), Application.Index(Res, 0, 1), 0)
                    If Not IsNumeric(r) Then ptr = ptr + 1: r = ptr: Res(ptr, 1) = a(i, j): Res(ptr, 4) = a(i, j)
                    Res(r, 2) = Res(r, 2) + 1
                    If bVis Then Res(r, 5) = Res(r, 5) + 1
               Next
          Next

          With .Range("B40")
               .Resize(1000, 5).ClearContents
               With .Resize(ptr, 5)
                    .Value = Res
                    .Resize(, 2).Sort .Range("B1"), xlDescending, Header:=xlNo
                    .Offset(, 3).Resize(, 2).Sort .Range("E1"), xlDescending, Header:=xlNo
               End With
          End With
     End With
End Sub
 
Upvote 0
in the yellow range with formulas, the integer part = number of times, the decimal part * 10,000 = the number himself.
Map1
ABCDEFGHIJKLM
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5visible
9Spain9915462891991546289
10France4721532671472153267
12USA1083071531108307153
13Spain437848523914378485239
14USA7499275781749927578
15Portugal7843814901784381490
16Italy553198735615531987356
21Italy148844894211488448942
22Usa255221201912552212019
23USA6842424241684242424
25Italy7528881591752888159
26Spain49100272757149100272757
27Spain4346823681434682368
28France732030237417320302374
29Portugal8129854611812985461
30France216964979512169649795
31
32
33
34
35
36
37
38
39VBA - AllVBA - filteredallfiltered
40444414,00784,0004
4178443324,00563,0081
4256478334,00043,0078
4389352343,00893,0052
448327353,00813,0043
4520381363,00523,0027
4643324373,00433,0024
4752399283,00272,0099
4827315293,00242,0089
49813462103,00202,0088
50243892
Blad1
Cell Formulas
RangeFormula
B9:F10,B12:F16,B21:F23,B25:F30B9=RANDBETWEEN(1,100)
G9:G10,G12:G16,G21:G23,G25:G30G9=(SUBTOTAL(102,B9:F9)>0)+0
I9:M10,I25:M30,I21:M23,I12:M16I9=+$G9*B9
H40:H49H40=SEQUENCE(10)
I40:I49I40=LARGE(COUNTIF($B$9:$F$30,SEQUENCE(100))+SEQUENCE(100)/10000,H40)
J40:J49J40=LARGE(COUNTIF($I$9:$M$30,SEQUENCE(100))+SEQUENCE(100)/10000,H40)
Dynamic array formulas.
 
Upvote 0
See if this is what you want.

With your Excel versions, formulas to be confirmed with Ctrl+Shift+Enter, not just Enter

drom.xlsm
ABCDEF
41st More Frecuent Number:88
52nd More Frecuent Number:1
63rd More Frecuent Number:33
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5
9Spain335888891
10France8776353697
11UK8349165588
12USA7321003881
13Spain762564421
14USA8085401280
15Portugal95343248
16Italy6100105075
17Belgium5041884326
18Mexico853276886
19Mexico2382975723
20uk4754676352
21Italy677913241
22Usa51368781
23USA3953868699
24UK9787515233
25Italy9644103357
26Spain7336482174
27Spain8810351364
28France188481270
29Portugal6468584993
30France175437543
31
32
Sheet1
Cell Formulas
RangeFormula
D4D4=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,Table3[[VALUES1]:[VALUES5]]))
D5D5=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,Table3[[VALUES1]:[VALUES5]])))
D6D6=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,IF(Table3[[VALUES1]:[VALUES5]]<>D5,Table3[[VALUES1]:[VALUES5]]))))
Press CTRL+SHIFT+ENTER to enter array formulas.


In my table above
88 occurs 4 times
1 occurs 4 times
33 occurs 3 times


After filtering for France & Italy

drom.xlsm
ABCDEF
41st More Frecuent Number:75
52nd More Frecuent Number:10
63rd More Frecuent Number:1
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5
10France8776353697
16Italy6100105075
21Italy677913241
25Italy9644103357
28France188481270
30France175437543
31
32
Sheet1
Cell Formulas
RangeFormula
D4D4=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,Table3[[VALUES1]:[VALUES5]]))
D5D5=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,Table3[[VALUES1]:[VALUES5]])))
D6D6=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,IF(Table3[[VALUES1]:[VALUES5]]<>D5,Table3[[VALUES1]:[VALUES5]]))))
Press CTRL+SHIFT+ENTER to enter array formulas.


75 occurs 3 times
10 occurs 2 times
1 occurs 2 times

If there are no numbers that occur multiple times, the formula returns #N/A. Example

drom.xlsm
ABCDEF
41st More Frecuent Number:75
52nd More Frecuent Number:43
63rd More Frecuent Number:#N/A
7
8CountryVALUES1VALUES2VALUES3VALUES4VALUES5
10France8776353697
28France188481270
30France175437543
31
32
Sheet1
Cell Formulas
RangeFormula
D4D4=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,Table3[[VALUES1]:[VALUES5]]))
D5D5=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,Table3[[VALUES1]:[VALUES5]])))
D6D6=MODE(IF(SUBTOTAL(102,OFFSET(Table3[VALUES1],ROW(Table3[VALUES1])-ROW(Table3[#Headers])-1,,1,5))>0,IF(Table3[[VALUES1]:[VALUES5]]<>D4,IF(Table3[[VALUES1]:[VALUES5]]<>D5,Table3[[VALUES1]:[VALUES5]]))))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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