LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #16   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 7,247
Default Ranges and Arrays in Excel VBA

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
comparing ranges/arrays asaylor Excel Worksheet Functions 7 June 13th 06 11:53 PM
Elements and Arrays in Excel Lighthouseman Excel Worksheet Functions 6 February 1st 06 10:06 AM
Confused about arrays and ranges in functions Llurker Excel Worksheet Functions 0 July 7th 05 05:44 AM
EXCEL ARRAYS & IF STATEMENTS Dan Excel Worksheet Functions 1 May 24th 05 07:02 PM
arrays in excel Dan Excel Worksheet Functions 9 May 24th 05 07:01 PM


All times are GMT +1. The time now is 08:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"