[英]VBA: how to pass a string array from an function
I can't handle the array in combination with an function very well.我不能很好地处理数组与函数的结合。 i want to edit an string array in an external function.我想在外部函数中编辑字符串数组。 (also redim it later with preserve) But in my test i get an error: (稍后也用保留重新修改它)但在我的测试中我得到一个错误:
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
You are not returning your array:你没有返回你的数组:
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
or as the comments have stated change to sub and do byRef:或如评论所述更改为 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
Return the array by reference :通过引用返回数组:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.