multidimensional array not getting passed from function

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
In the following function I build a 5 by 2 array. the syntax is

Code:
function alpha( code)

dim ancestors(5,2)
(code)

dim result(11)
result(9) = ancestors

end function

sub 

dim ancestors5(5,2)
alpha_result = alpha( code )

ancestors5(5,2) = alpha_result(9)


end sub


As you can see from the screen shots there is definitely something in the alpha_result(9) array but it is not getting passed on to the ancestors5 array.

Screenshot2014-10-05at105328PM_zps955e29da.png

Screenshot2014-10-05at105359PM_zps532709dd.png
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Your function doesn't return a value and the variable it uses is declared locally to the function so loses its value as soon as the function ends. I suggest you read up on variable scope and lifetime - Chip Pearson's website is a good place to start.
 
Upvote 0
It doesn't lose its value as soon as the function ends. If you look at the screen shots you'll see that the function has ended and alpha_result(9) is still readable.
 
Upvote 0
Your function makes no reference to alpha_result, which makes it pretty hard to follow what you think should be happening.
 
Upvote 0
The function doesn't return anything readable.

Code:
Function F_alpha(code)
    F_alpha = code(5, 2)
End Function

Sub tst()
    Dim ancestors5(5, 2)
    ancestors5(5, 2) = "yourself"

    MsgBox F_alpha(ancestors5)
End Sub
 
Upvote 0
The function doesn't return anything readable.

Code:
Function F_alpha(code)
    F_alpha = code(5, 2)
End Function

Sub tst()
    Dim ancestors5(5, 2)
    ancestors5(5, 2) = "yourself"

    MsgBox F_alpha(ancestors5)
End Sub


Then how do you explain the screen shots where clearly something readable is returned?
 
Upvote 0
Apart from the other points, you put the value into ancestors5(5, 2) but your screenshot shows ancestors5(1).
 
Upvote 0
Apart from the other points, you put the value into ancestors5(5, 2) but your screenshot shows ancestors5(1).

Do you mean where it says"

Code:
if ancestors5(1,1) <> "" then

If so that just means that if the first member of the ancestors5 arrays is blank then don't do into that loop.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,148
Members
448,552
Latest member
WORKINGWITHNOLEADER

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