Oorang
Well-known Member
- Joined
- Mar 4, 2005
- Messages
- 2,071
Hello everyone,
I was recently using mode for a report and I encountered the following problems.
MODE:
-Cannot be used with columnar refrences.
-Mode will show you only one mode even if it should by bimodal etc. Essientially returning a "wrong" answer.
-If there is more than one mode, the selection process is apparently erratic. After research I discovered it shows the number whose first occurance has the lowest row value. Or if all have equal row value then lowest column value wins.
Deciding I wanted a better mode, I set about making one. This is what I have so far, I would very much welcome comments and ideas to improve.
MODE2:-Can be used with columnar refrences. (This dramaticlly increases calculation time so I included an optional switch to abort calculation after reaching so many blank cells.
- Will show ALL modes.
-Instead of returning an error for no mode. If actually says "No Mode"
I was recently using mode for a report and I encountered the following problems.
MODE:
-Cannot be used with columnar refrences.
-Mode will show you only one mode even if it should by bimodal etc. Essientially returning a "wrong" answer.
-If there is more than one mode, the selection process is apparently erratic. After research I discovered it shows the number whose first occurance has the lowest row value. Or if all have equal row value then lowest column value wins.
Deciding I wanted a better mode, I set about making one. This is what I have so far, I would very much welcome comments and ideas to improve.
MODE2:-Can be used with columnar refrences. (This dramaticlly increases calculation time so I included an optional switch to abort calculation after reaching so many blank cells.
- Will show ALL modes.
-Instead of returning an error for no mode. If actually says "No Mode"
Code:
Option Explicit
Function MODE2(RNG As Range, Optional C As Long)
'Written by Aaron Bush 11/09/05
'Free for public use.
Dim X As Long, LC As Long
Dim MC As Double
Dim CV() As String, MV As String
Dim Cll As Object
ReDim CV(0)
If C = 0 Then C = 65536
For Each Cll In RNG
If Trim(Cll.Value) = "" Then LC = LC + 1
If CV(0) = Empty And Trim(Cll.Value) <> "" Then CV(0) = Cll.Value & "-0"
If IsNumeric(Trim(Cll.Value)) And Trim(Cll.Value) <> "" Then
For X = LBound(CV) To UBound(CV)
If Trim(Mid(CV(X), 1, InStr(CV(X), "-") - 1)) = Trim(Cll.Value) Then
CV(X) = Mid(CV(X), 1, InStr(CV(X), "-")) & CLng(Mid(CV(X), InStr(CV(X), "-") + 1)) + 1
Exit For
End If
Next X
End If
If X > UBound(CV) Then
ReDim Preserve CV(X)
CV(X) = Cll.Value & "-" & 1
End If
If LC = C Then Exit For
Next Cll
LC = 0
For X = LBound(CV) To UBound(CV)
If CLng(Mid(CV(X), InStr(CV(X), "-") + 1)) > MC Then
MV = CLng(Mid(CV(X), 1, InStr(CV(X), "-") - 1))
MC = CLng(Mid(CV(X), InStr(CV(X), "-") + 1))
Else
If CLng(Mid(CV(X), InStr(CV(X), "-") + 1)) = MC Then
LC = LC + 1
MV = MV & ", " & CLng(Mid(CV(X), 1, InStr(CV(X), "-") - 1))
End If
End If
Next X
If LC = UBound(CV) Then
MODE2 = "No Mode"
Else
MODE2 = MV
End If
End Function