Home |
Search |
Today's Posts |
#16
![]()
Posted to microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Try something like the following. Arr1 is the input array of numbers.
Populate that array any way you want. At the end of the procedure, Arr2 will contain the count of each element in Arr1, such that Arr2(N) is the count of elements equal to N in Arr1. In other words Arr2(Arr1(N)) equals the count of the element in Arr1(N). In the example below, Arr2(3) equal 3 because there are three 3's in Arr1. Arr2(Arr1(3)) returns 1 because Arr1(3) is a five, and there is only one 5 in Arr1. Sub ArrayElementCounts() Dim Arr1(1 To 10) As Long Dim Arr2() As Long Dim N As Long Dim ArrMax As Long Dim ArrMin As Long '''''''''''''''''''''''''''''''''''' ' Populate Arr1 any way you want. '''''''''''''''''''''''''''''''''''' Arr1(1) = 1 Arr1(2) = 3 Arr1(3) = 5 Arr1(4) = 3 Arr1(5) = 2 Arr1(6) = 3 Arr1(7) = 2 Arr1(8) = 7 Arr1(9) = 7 Arr1(10) = 9 ''''''''''''''''''''''''''''''''''''' ' Get the Min and Max values of Arr1 ''''''''''''''''''''''''''''''''''''' ArrMin = Application.Min(Arr1) ArrMax = Application.Max(Arr1) ''''''''''''''''''''''''''''''''''''' ' Redim Arr2 from Min to Max ''''''''''''''''''''''''''''''''''''' ReDim Arr2(ArrMin To ArrMax) For N = LBound(Arr1) To UBound(Arr1) ''''''''''''''''''''''''''''''''''''''''''' ' Increment element Arr1(N) of Arr2 ''''''''''''''''''''''''''''''''''''''''''' Arr2(Arr1(N)) = Arr2(Arr1(N)) + 1 Next N '''''''''''''''''''''''''''''''''''''''' ' Display the results. Do whatever you ' want with the result array Arr2. '''''''''''''''''''''''''''''''''''''''' For N = LBound(Arr2) To UBound(Arr2) Debug.Print "Value: " & N, "Count: " & Arr2(N) Next N End Sub -- Cordially, Chip Pearson Microsoft MVP - Excel, 10 Years Pearson Software Consulting www.cpearson.com (email on the web site) "a.riva@UCL" wrote in message ps.com... Thanks for all the suggestions! Now I have an other question... I have my usual option-based-1 array1 in VBA, which contains x numbers. Some of them are repeated. What I would like to do is creating an other option-based-1 array, let's call it array2, which contains the numbers of occurrences of each of the repeated elements of array1 within array1... I'm struggling to find a solution... For example: option-based-1 array1 is (1, 2, 4, 6, 5, 4, 7, 2, 3, 2, 3) -- I cannot sort the array. I think that the procedure should do the following operation: it detects how many items are repeated in "array1", and for each of this repeated items stores in a new array "array2" a number corresponding to the number of its occurrences. For example, in array1 the procedure detects that there are n=3 items which occur more than once (they are "2", "4" and "3"). Then it ReDims array2 (1 to n), and for i=1 to n it gives to array2(i) the values of: i=1 -- array2(1) = 3 (occurrences of "2"), i=2 -- array2(2) = 2 (occurrences of "4"), i=3 -- array2(3) = 2 (occurrences of "3"). Can somebody help me? Thanks in advance :-) Antonio. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
comparing ranges/arrays | Excel Worksheet Functions | |||
Elements and Arrays in Excel | Excel Worksheet Functions | |||
Confused about arrays and ranges in functions | Excel Worksheet Functions | |||
EXCEL ARRAYS & IF STATEMENTS | Excel Worksheet Functions | |||
arrays in excel | Excel Worksheet Functions |