multidimensional array not getting passed from function

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,550
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
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,166
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,550
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,166
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Your function makes no reference to alpha_result, which makes it pretty hard to follow what you think should be happening.
 

snb_

Well-known Member
Joined
Nov 9, 2009
Messages
567

ADVERTISEMENT

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
 

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,550
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?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,166
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

Apart from the other points, you put the value into ancestors5(5, 2) but your screenshot shows ancestors5(1).
 

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,550
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,166
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
but your screenshot shows ancestors5(1).

I'm not talking about your code (since what you posted clearly isn't your actual code).
 

Watch MrExcel Video

Forum statistics

Threads
1,123,493
Messages
5,601,998
Members
414,490
Latest member
Rip181

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
Top