I'm attempting to use an array created from a user-defined function in another user-defined function. For this exercise, I have two separate functions.
The first function will create an array from a range input and sum the values (yes I know there's an excel built-in sum function but this is just for exercise).
The second function will call upon the array created in the first function and sum the values with a second range input. See the following code.
Function first_funct(list_1 As range) As Double
Dim extent As Integer
extent = list_1.rows.Count
Dim main_array() As Double
ReDim main_array(1 To extent)
Dim i As Integer
first_funct = 0
For i = 1 To extent
main_array(i) = list_1(i).Value
'main_array will be used again in second function
first_funct = first_funct + main_array(i)
Next i
End Function
Function second_funct(list_2 As range) As Double
Dim extent As Integer
extent = list_2.rows.Count
'Assume the extent of list_2 is equal to extent of list_1 in first function.
Dim i As Integer
second_funct = 0
For i = 1 To extent
second_funct = second_funct + main_array(i) + list_2(i).Value
'How do I call upon main_array created from list_1 in the first function?
Next i
End Function
My question is: how do I use the main_array
created in the first function so that I can use it for the second.
In reality, I have hundreds of cells that need to be calculated and creating the same array for each one is inefficient. I would like to create an array one time (in this case function #1) and use a simpler function (function #2) that will call upon the saved array, saving me many lines of code and time.
Thanks in advance.