Transfer Dictionary data to an Array, but getting "subscript out of range"

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
My code is running into a Run-time error 9 (Subscript out of range).

When the code is debugged, the line printed in red is highlighted.

Here is my function:

Code:
Function populateDict(thearraY As Variant, retunArray As Variant) As Variant
Dim i As Integer

Dim key As Variant

    For i = 1 To UBound(thearraY)
    
        If theDict.Exists(thearraY(i, 2)) Then
        
        theDict(thearraY(i, 2)) = theDict(thearraY(i, 2) + thearraY(i, 8))
        
        
        Else
        
        theDict.Add key:=thearraY(i, 2), Item:=thearraY(i, 8)
    
        End If
    
    Next i
    
    i = 1
    
    ReDim Returnarry(UBound(thearraY), 2)
    
    For Each key In theDict.Keys
          [COLOR=#ff0000]
              returnArray(i, 1) = key[/COLOR]

              returnArray(i, 2) = theDict(key)

              i = i + 1             
            
              
        Next key    
    
End Function


At the error, i = 1, Key = "GEMS" theDict(key) = "Percentage", theDict has a count of 566, and the ubound(thearray) figure is 571.

I can't figure out why I am getting a out of range error.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
In your steps, you didn't mention if you are initializing the array by assigning it some other array, resizing it with ReDim [Preserve]. Only declaring array creates a zero sized array. Unless you initialize it, it has size 0.

The ReDim statement in populateDict is not resizing returnArray; it is resizing Returnarry (notice the difference in spelling). After resizing Returnarry, you are trying to assign values to returnArray. Did you mean to resize variable returnArray instead?

populateDict is actually taking two parameters in your code. What's the second one for? I don't see retunArray being used anywhere.
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,058
Members
449,206
Latest member
Healthydogs

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