The msot common in an array

ShvDK

Board Regular
Joined
Sep 5, 2003
Messages
92
I'm creating a dynamic array declared as variant on the fly and then redim preserve it each time I put in a record. The records are all integers

When my array is complete (it has only 1 dimension but thoussands of records) I have to find out which of the records that is the most common within the array.

Anyone who has an idea - I'll prefer to have the 'common record search' in VBA instead of just populating the data in a worksheet!

Thanx in advance!

ShvDK
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi

Look at Application.WorksheetFunction.Mode - this is untested by the way, but I presume it will work.

It does :)

Code:
Sub test()
Dim myarray(1 To 10), i As Integer
For i = 1 To 10
    myarray(i) = Range("A" & i).Value
Next i
MsgBox WorksheetFunction.Mode(myarray)
End Sub
 
Upvote 0
Agree.. it seems to work but I get the error msg. 'Invalid procedure call or argument'. Your array is predefined as 1 to 10 - mine is redim'ed and preserved... could that be the reason for the error?
 
Upvote 0
OOOOuuuuppppsss... sorry guys - I'll forgot that my array was empty due to an error in my code. When I refilled my array the worksheetfunction.mode worked!

Thanx for your assistance!
 
Upvote 0
Hi,

take care, sometimes Mode can not be calculated
try data serie
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Code:
Sub test()
Dim myarray(1 To 10), i As Integer
 
    For i = 1 To 10
    myarray(i) = Range("A" & i).Value
    Next i
 
On Error Resume Next
 
i = WorksheetFunction.Mode(myarray)
    If Err Then
    MsgBox "Mode could not be calculated", vbCritical, "ERROR"
    Else
    'proceed
    End If
 
On Error GoTo 0
 
End Sub
kind regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,028
Members
448,940
Latest member
mdusw

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