2014-10-06 72 views
0

在下面的函數中,我構建了一個5乘2的數組。語法是多維數組未從函數傳遞(excel VBA)

[代碼]

function alpha(code) 

dim ancestors(5,2) 
(code) 

dim result(11) 
result(8) = code 
result(9) = ancestors 
result(10) = code 

alpha = result 


end function 

sub 

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

ancestors5(5,2) = alpha_result(9) 


end sub 

[/代碼]

一個更簡單的代碼如下:

function alpha(code) as variant 

dim arr(5,2) 

result(1) = arr 
result(2) = something_else 
alpha = arr 

end function 

sub 

dim arr2(5,2) 

call = alpha(code) 

arr2(5,2) = call(1) 

end sub 

溫度=

正如你可以看到從屏幕截圖中,alpha_result(9)數組中肯定存在某些東西,但它不會傳遞給a ncestors5數組。

enter image description here

enter image description here

+0

'昏暗的祖先(5,2)'實際上創建了一個6X3陣列,因爲默認LBOUND是零,你聲明ubounds爲5和2。所以,你得到0-5和0-2 。通過一個可編輯的問題示例來更新您的問題會很有用:現在很難跟蹤。你的'alpha'函數不會返回任何東西,例如... – 2014-10-06 03:29:33

+0

我試圖將祖先聲明爲祖先(6,3),但沒有做任何事情。 – user147178 2014-10-06 03:33:22

+0

您需要改進您的示例代碼,以便人們可以看到您想要在那裏做什麼。代碼稍微改進了 – 2014-10-06 03:37:58

回答

1

這是什麼意思?

Function Alpha() 
Dim a(5, 2) 
Dim b(11) 

    a(1, 1) = "test" 'e.g. 
    b(9) = a 

    Alpha = b 
End Function 

Sub tester() 
    Dim arr, arr2 ' Variants 
    Dim arr3(5, 2) ' declare empty array with dimensions 0-5, 0-2 

    arr = Alpha() '>> arr is now a 1-d array with dimensions 0-11 

    MsgBox arr(9)(1, 1) '>> "test" value from 2-d array stored at 
         ' arr(9) 
    'or... 
    arr2 = arr(9)  'arr2 is now a 2-d array with dimensions 0-5, 0-2 
    MsgBox arr2(1, 1) '>> "test" 

    ' it's unclear what you *want* to happen here: 
    arr3(5, 2) = arr(9) '<< here you're assigning the 2-d array 
         ' stored in arr(9) to the element of 
         ' arr3() at 5,2 
End Sub