最好不使用循环,是否可以填充空数组的内容,让我们用其他两个数组的内容称之为C
,让我们调用它们{{1 }和A
?
B
我做错了什么?
答案 0 :(得分:3)
最好不使用循环,是否可以填充空数组的内容
就像我在评论中提到的那样,可能将两个数组合并到第三个数组 而不使用循环 。
执行此操作的关键是您不使用Double
作为数组类型,而是使用Variant
。见这个例子。
Option Explicit
Sub Sample()
Dim A(1 To 2) As Variant
Dim B(1 To 2) As Variant
Dim C As Variant
Dim Ub_A As Long, Ub_B As Long, i As Long
Dim sA As String, sB As String, sAB As String
'~~> Assign sample data to array A and B
A(1) = 1: A(2) = 2: B(1) = 3: B(2) = 4
Ub_A = UBound(A): Ub_B = UBound(B)
sA = "{" & Join(A, ",") & "},"
sB = "{" & Rept("0,", Ub_A) & Join(B, ",") & "},"
sAB = "{" & Rept("1,", Ub_A) & Rept("2,", Ub_B)
sAB = Left(sAB, Len(sAB) - 1) & "},"
'~~> Construct your C Array
C = Evaluate("Choose(" & sAB & sA & sB & ")")
'~~> For testing purpose only to check the elements of C Array
For i = LBound(C) To UBound(C)
Debug.Print ">>"; C(i)
Next i
End Sub
Private Function Rept(s As String, j As Long) As String
Rept = Replace(Space(j), " ", s)
End Function
<强>截图强>
答案 1 :(得分:1)
如果我在你的鞋子上,我会考虑使用这样的范围:
Sub sample()
Dim A
Dim B
Dim C
Dim i As Long, j As Long
A = Array(1, 2, 3, 4)
B = Array(5, 6, 7, 8)
Range("A1:A" & UBound(A) + 1) = Application.Transpose(A)
Range("B1:B" & UBound(B) + 1) = Application.Transpose(B)
C = Application.Transpose(Range("A1:B" & UBound(B) + 1))
'~~> Just to test the array elements
For i = 1 To UBound(C, 1)
For j = 1 To UBound(C, 2)
Debug.Print C(i, j)
Next
Next
End Sub
不是很整洁,但它会给你你想要的东西
此外,Siddhart是正确的,将变量声明为Variant
类型。