在VBA中,可以通过使用数组函数来从两个数组获得单个数组输出。以下是一种实现方法:
Function CombineArrays(arr1() As Variant, arr2() As Variant) As Variant()
Dim result() As Variant
ReDim result(1 To UBound(arr1))
Dim i As Long
For i = 1 To UBound(arr1)
result(i) = arr1(i) + arr2(i)
Next i
CombineArrays = result
完整的代码如下:
Function CombineArrays(arr1() As Variant, arr2() As Variant) As Variant()
Dim result() As Variant
ReDim result(1 To UBound(arr1))
Dim i As Long
For i = 1 To UBound(arr1)
result(i) = arr1(i) + arr2(i)
Next i
CombineArrays = result
End Function
使用示例:
Sub TestCombineArrays()
Dim arr1(1 To 3) As Variant
Dim arr2(1 To 3) As Variant
Dim combined() As Variant
arr1(1) = 1
arr1(2) = 2
arr1(3) = 3
arr2(1) = 4
arr2(2) = 5
arr2(3) = 6
combined = CombineArrays(arr1, arr2)
' 输出结果数组
Dim i As Long
For i = 1 To UBound(combined)
Debug.Print combined(i)
Next i
End Sub
这样,通过调用CombineArrays函数,我们可以从两个输入数组获得单个数组输出。
领取专属 10元无门槛券
手把手带您无忧上云