Vba 如何用另一个数组的内容填充数组的一列?

Vba 如何用另一个数组的内容填充数组的一列?,vba,excel-2010,Vba,Excel 2010,最好不使用循环,是否可以用另外两个数组的内容来填充一个空数组的内容,我们称之为C,我们称之为A和B Dim A() As Double Dim B() As Double ReDim A(1 To 100,1 To 1) ReDim B(1 To 100,1 To 1) ' fill A and B with stuff... Dim C As Double ' I now want "A" to form the first column and "B" to form the sec

最好不使用循环,是否可以用另外两个数组的内容来填充一个空数组的内容,我们称之为
C
,我们称之为
A
B

Dim A() As Double
Dim B() As Double
ReDim A(1 To 100,1 To 1)
ReDim B(1 To 100,1 To 1)


' fill A and B with stuff...

Dim C As Double

' I now want "A" to form the first column and "B" to form the second column of array C

C(1 To 100, 1) = A(1 To 100, 1) ' Compile error at '=': Expected end of statement
C(1 To 100, 2) = B(1 To 100, 1) ' Compile error at '=': Expected end of statement
我做错了什么

最好不使用循环,是否可以填充空数组的内容

正如我在评论中提到的,可以将两个数组组合成第三个数组,而无需使用循环

要做到这一点,关键是不要使用
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
屏幕截图

最好不使用循环,是否可以填充空数组的内容

正如我在评论中提到的,可以将两个数组组合成第三个数组,而无需使用循环

要做到这一点,关键是不要使用
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
屏幕截图


如果我是你的话,我会考虑使用这样的范围:

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是正确的,将变量声明为<代码>变体Type。

< P>我会考虑使用这样的范围,如果我处在你的立场:

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
type。

VBA没有这种数组赋值。是的,这是可能的。等一下。发布replyVBA没有这种数组分配。是的,这是可能的。等一下。在西德哈特一号发布回复。但我敢打赌,OP现在可以使用循环了。;-)Lol@TimWilliams:我更喜欢环。更好地控制代码:)谢谢Siddharth,你肯定回答了这个问题!但蒂姆是对的!西德哈特,不错。但我敢打赌,OP现在可以使用循环了。;-)Lol@TimWilliams:我更喜欢环。更好地控制代码:)谢谢Siddharth,你肯定回答了这个问题!但蒂姆是对的!