我不能很好地處理陣列與函式的結合。我想在外部函式中編輯字串陣列。(稍后也用保留重新修改它)但在我的測驗中我收到一個錯誤:
Sub test()
Dim test() As Variant
Dim testnew() As Variant
ReDim Preserve test(1 To 4)
test(1) = "one"
test(2) = "two"
test = testfunc(test)
Debug.Print test(3)
End Sub
Function testfunc(test() As Variant) As Variant
test(3) = "three"
End Function
uj5u.com熱心網友回復:
你沒有回傳你的陣列:
Sub test()
Dim test() As Variant
Dim testnew() As Variant
ReDim Preserve test(1 To 4)
test(1) = "one"
test(2) = "two"
test = testfunc(test)
Debug.Print test(3)
End Sub
Function testfunc(test() As Variant) As Variant
test(3) = "three"
testfunc = test
End Function
或如評論所述更改為 sub 并執行 byRef:
Sub test()
Dim test() As Variant
Dim testnew() As Variant
ReDim Preserve test(1 To 4)
test(1) = "one"
test(2) = "two"
testfunc test
Debug.Print test(3)
End Sub
Sub testfunc(ByRef test() As Variant)
test(3) = "three"
End Sub
uj5u.com熱心網友回復:
通過參考回傳陣列:
Public Sub test()
Dim test() As Variant
Dim testnew() As Variant
ReDim Preserve test(1 To 4)
test(1) = "one"
test(2) = "two"
testfunc test
Debug.Print test(3)
End Sub
Public Sub testfunc(test() As Variant)
test(3) = "three"
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/350314.html
