MODE not working with named range

kweaver

Well-known Member
Joined
May 12, 2009
Messages
2,934
Office Version
  1. 365
  2. 2010
I've created a named range for a block of data. AVERAGE, MEDIAN, COUNT, COUNTIF, and STDEV all work with the named range.
MODE, however, returns a #VALUE error. If I use MODE without the named range by dragging over the data (need to use CTRL as I drag because the data is not contiguous), it works.
There is only one MODE for the data.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Appears MODE doesn't work with non-contiguous named ranges.

You can write a custom function as a workaround.

Book1
ABCDEF
1
211#VALUE!
3222
4322
542
624
725
836
9
Sheet1
Cell Formulas
RangeFormula
E2E2=MODE(data)
E3E3=MODE(A2:A8,C2:C8)
E4E4=myMode(data)
Named Ranges
NameRefers ToCells
data=Sheet1!$A$2:$A$8,Sheet1!$C$2:$C$8E2:E4


VBA Code:
Option Explicit

Function myMode(data As Range) As Double
    Dim i As Long, c
    For Each c In data
        i = i + 1
    Next
    ReDim a(1 To i, 1 To 1)
    i = 0
    For Each c In data
        i = i + 1
        a(i, 1) = c
    Next
    myMode = Application.Mode(a)
End Function
 
Upvote 0
shorter, more efficient version:

VBA Code:
Function myMode(data As Range) As Double
    Dim i As Long, c
    ReDim a(1 To data.Count, 1 To 1)
    For Each c In data
        i = i + 1
        a(i, 1) = c
    Next
    myMode = Application.Mode(a)
End Function
 
Upvote 0
Appears MODE doesn't work with non-contiguous named ranges.

You can write a custom function as a workaround.

Book1
ABCDEF
1
211#VALUE!
3222
4322
542
624
725
836
9
Sheet1
Cell Formulas
RangeFormula
E2E2=MODE(data)
E3E3=MODE(A2:A8,C2:C8)
E4E4=myMode(data)
Named Ranges
NameRefers ToCells
data=Sheet1!$A$2:$A$8,Sheet1!$C$2:$C$8E2:E4


VBA Code:
Option Explicit

Function myMode(data As Range) As Double
    Dim i As Long, c
    For Each c In data
        i = i + 1
    Next
    ReDim a(1 To i, 1 To 1)
    i = 0
    For Each c In data
        i = i + 1
        a(i, 1) = c
    Next
    myMode = Application.Mode(a)
End Function
Thanks...Apparently NOT...as I discovered and pointed out in my post. In this particular situation, I can't use a macro or UDF, but appreciate your suggestion.
 
Upvote 0
You can trick Excel into doing it:

Book1
ABCDE
1
211#VALUE!
3222
4322
5432
656
767
8
Sheet1
Cell Formulas
RangeFormula
E2E2=MODE(data)
E3E3=MODE(A2:A7,C2:C7)
E4E4=MODE(INDEX(data,,,1),INDEX(data,,,2))
E5E5=MODE(SMALL(data,ROW(INDIRECT("1:"&COUNT(data)))))


If you know how many different areas are in the range, then try the E4 formula. If not, try the E5 formula.
 
Upvote 0
Marcelo: fantastic! I didn't think to try that. Perfect!!
 
Upvote 0
Marcelo: fantastic! I didn't think to try that. Perfect!!

kweaver,

I did some tests and noticed that this formula can have problems. It should only be used when there are no values between non-contiguous ranges. For example if Data contains the ranges A2:A7 and C2:C7 the formula will count values in B2:B7 as well, because the union Data:Data results in the range A2:C7...

Therefore, this formula, although elegant, is not recommended.

M.
 
Upvote 0
When I realized that a named range didn't work, I use this:
Code:
=MODE(M12:N12,C13:N16,C17:J17)
but your example works perfectly with the named range referencing that block.
 
Upvote 0
I can see that in your case the named range Data is composed by 3 ranges: M12:N12; C13:N16 and C17:J17. The formula you used is perfect, as it only counts values in the 3 ranges.

What I tried to say is that using the formula =MODE(Data:Data) values in C12:N12 and K17:N17, which don't actually belong to the named range Data, will be counted as well. This is because the union Data:Data results in C12:N17.

So be careful when using a formula that uses the union Data:Data as the argument of any function.

M.
 
Upvote 0

Forum statistics

Threads
1,215,506
Messages
6,125,194
Members
449,214
Latest member
mr_ordinaryboy

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