create array by using a function

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
I am getting a mismatch error at the line highlighted in red. The error occurs after the function is ran. I am confused because I created both items variants. How could there be a mismatch?




Code:
Option Explicit
Option Base 1
 
 
Dim theDict As New Scripting.dictionary


Sub Create_ownership()

    Dim capitalOwnerArray() As Variant       
    
    [COLOR=#ff0000][B]capitalOwnerArray = populateDict(capitalArraY)[/B][/COLOR]
    
End Sub

Function populateDict(thearraY As Variant) As Variant

Dim i As Integer

Dim key As Variant

Dim returnarray() 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
    
  ReDim returnarray(UBound(thearraY), 2)
  
  Erase thearraY
    
     
   i = 1
    
    For Each key In theDict.Keys
          
              returnarray(i, 1) = key
              returnarray(i, 2) = theDict(key)
              i = i + 1
              
        Next key
        
    thearraY = returnarray
    
    
End Function
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
What is capitalArraY and how is it populated?
 
Upvote 0
What is capitalArraY and how is it populated?
In addition to RoryA's questions, I don't understand the construction of your function. You seem to have constructed it like a subroutine, not a function. As a last step in a function, you assign the return value or object to the function's name so that it assigned or used by whatever line of code called it... you are not doing that , instead, you assigned the return object back to the argument itself but then you try and get at that object via the function's name in the calling line of code... since you never assigned anything to the function's name, its value is Empty.
 
Upvote 0
I am new a functions so maybe I guess I don't understand how to construct them.

I want to populate a array with data from a dictionary. I find arrays easier to work with.

I want to populate 5 arrays. Normally, I would write the same code 5 times for each array, but I read it would be more efficient if I used a function.
 
Upvote 0

Forum statistics

Threads
1,215,220
Messages
6,123,698
Members
449,117
Latest member
Aaagu

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