VBA Code for numbering an array

way15126

New Member
Joined
Mar 26, 2020
Messages
9
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

I am looking for a VBA code to generate the following arrays:

Input array:
VBA Code:
array1(12 , 5 , 12 , 5 , 5 , 3 , 4 , 3 , 4)
Output array:
VBA Code:
array2( 1,  1 ,  2 , 2 , 3 , 1 , 1 , 2 , 2)

The output array numbers the number of times the respective array input has been shown before.
Eg.
array1(1) = 12, and is the first time its showing, thus array2(1) = 1
array1(2) = 5, and is the first time its showing, thus array2(2) = 1
array1(3) = 12, and is the second time its showing, thus array2(3) = 2
array1(4) = 5, and is the second time its showing, thus array2(4) = 2

and so on so forth.

I am thinking of considering the first x elements of the array through a loop, and counting the number of occurrences of that particular element in array1 and then storing it in array2, but I am unsure of how to consider only the first x elements of an array.

Appreciate any help at all! Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi.
Try this.
VBA Code:
Sub test()
    Dim array1, array2, i%
    array1 = Array(12, 5, 12, 5, 5, 3, 4, 3, 4)
    ReDim array2(LBound(array1) To UBound(array1))
    With CreateObject("Scripting.Dictionary")
        For i = LBound(array1) To UBound(array1)
            krt = array1(i)
            .Item(krt) = .Item(krt) + 1
            array2(i) = .Item(krt)
        Next i
    End With
    
    MsgBox Join(array1, ", ") & vbCr & Join(array2, ", ")
End Sub
 
Upvote 0
Solution
Hi.
Try this.
VBA Code:
Sub test()
    Dim array1, array2, i%
    array1 = Array(12, 5, 12, 5, 5, 3, 4, 3, 4)
    ReDim array2(LBound(array1) To UBound(array1))
    With CreateObject("Scripting.Dictionary")
        For i = LBound(array1) To UBound(array1)
            krt = array1(i)
            .Item(krt) = .Item(krt) + 1
            array2(i) = .Item(krt)
        Next i
    End With
   
    MsgBox Join(array1, ", ") & vbCr & Join(array2, ", ")
End Sub
Hi veyselemre,

Thanks! The code works perfectly.

way15126
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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