Reputation: 35
I'm new to excel, trying to calculate the mode of an array, but that array is stored in a single cell. For example, I'm storing an array(0,1,2,3,4,2,3,3,4) into cell A1 and trying to find the mode of that array. I tried MODE(A1) function, but it seems doesn't work. Is there any good recommendation? I heard it can be achieved by VBA, but I'm a noob to excel, is there any good and simple tutorial could achieve this goal? Appreciate if someone could help with this, thx:)
Upvotes: 1
Views: 163
Reputation:
There is a secret function called Evaluate that can take a formula constructed from concatenated strings and produce a result equivalent to that formula being used on a worksheet.
In your case, you want to build a formula that treats your string of delimited numbers as an array of real numbers so the target would look like this.
=MODE(0,1,2,3,4,2,3,3,4)
A User Defined Function can bring this functionality to the worksheet.
Option Explicit
Function arrayMode(str As String)
arrayMode = Application.Evaluate("MODE(" & str & ")")
End Function
Evaluate can also be accessed through a Defined Name. Select B2 then choose Formulas, Defined Names, Define Name. Give it a name (e.g. ARRMODE) and use the folllowing for the Referes to:
=Evaluate("MODE("&$A2&")")
Click OK.
Use ARRMODE like a worksheet function. The reference to the cell in column A on the same row is built in.
=ARRMODE
Upvotes: 3
Reputation: 96773
In A2 enter:
=IFERROR(--TRIM(MID(SUBSTITUTE($A1,",",REPT(" ",999)),COLUMNS($A:A)*999-998,999)),"")
and copy across. Then in A3 enter:
=MODE(2:2)
(You can avoid all the "helper" cells by using the EVALUATE() function)
Upvotes: 0