如何将1D阵列复制到另一个1D阵列和2D阵列?

时间:2023-02-08 07:46:45

Suppose I have an array (1,2,3,4), then I want to copy it to another 1D array and 2D array. I use this sub-routine:

假设我有一个数组(1,2,3,4),那么我想将它复制到另一个1D数组和2D数组。我用这个子程序:

Sub CopyArray()
    Dim Arr1(), Arr2()
    ReDim Arr3(1 To m, 1 To n)

    Arr1 = Array(1, 2, 3, 4)

    For i = 1 To 4
        Arr2(i) = Arr1(i)
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

It kept getting an error "subscript out of range". I also tried

它不断得到“下标超出范围”的错误。我也试过了

Sub CopyArray()
    Dim Arr1(), Arr2()

    Arr1 = Array(1, 2, 3, 4)

    For i = 1 To 4
        Arr2(i) = Arr1(i)
    Next i

End Sub

or

要么

Sub CopyArray()
    Dim Arr1()
    ReDim Arr3(1 To m, 1 To n)

    Arr1 = Array(1, 2, 3, 4)

    For i = 1 To 4
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

but none of them worked. How does one copy 1D array to another 1D array and 2D array properly?

但他们都没有工作。如何将1D阵列正确复制到另一个1D阵列和2D阵列?

4 个解决方案

#1


3  

Simplest way to copy one array to another in your case would be to declare the arrays as Variant. This way you will not have to loop

在您的情况下,将一个数组复制到另一个数组的最简单方法是将数组声明为Variant。这样你就不必循环了

Example of 1D array

一维数组的示例

Sub CopyArray()
    Dim x As Variant
    Dim y As Variant

    x = Array(1, 2, 3)

    y = x '<~~ Directly clone the array

    For i = LBound(y) To UBound(y)
        Debug.Print y(i)
    Next i
End Sub

Example of 2D Array

2D阵列的示例

Sub CopyArray()
    Dim x As Variant
    Dim y As Variant

    x = Array(1, 2, 3)

    y = Application.Transpose(x) '<~~ Transpose it

    For i = LBound(y) To UBound(y)
        Debug.Print y(i, 1)
    Next i
End Sub

#2


1  

First your i has to start at 0 because it is where arrays start. For example Arr1(1) = 2 and not 1 in your code. Secondly you have to Redim your second array so it has the same size as your first one. Here is a piece of code to help you out:

首先你的i必须从0开始,因为它是数组开始的地方。例如,Arr1(1)= 2而代码中不是1。其次,你必须Redim你的第二个数组,所以它与你的第二个数组大小相同。这是一段代码可以帮助您:

Dim Arr1(), Arr2()

Arr1 = Array(1, 2, 3, 4)
ReDim Arr2(0 To 3)

For i = 0 To 3
    Arr2(i) = Arr1(i)
Next i

#3


1  

The following code works fine:

以下代码工作正常:

Sub CopyArray()
    Dim Arr1(), Arr2(3)

    Arr1 = Array(1, 2, 3, 4)

    For i = 0 To 3
        Arr2(i) = Arr1(i)
    Next i

End Sub

Note that the declaration does not specify the number of elements in the array. Instead, it specifies the upper bound of the array. If your module does not contain an Option Base statement, the lower bound is assumed to be zero. So, the declaration above, Dim Arr2(3), is the same as

请注意,声明未指定数组中的元素数。相反,它指定数组的上限。如果您的模块不包含Option Base语句,则假定下限为零。因此,上面的声明,Dim Arr2(3),与之相同

Dim Arr2(0 To 3) As Variant

You may check that Arr1(1) is equal to 2, not 1 as you might think. Try to add the following line below the Next i to check each element of Arr1:

您可以检查Arr1(1)是否等于2,而不是您认为的1。尝试在Next i下面添加以下行来检查Arr1的每个元素:

Debug.Print Arr1(0), Arr1(1), Arr1(2), Arr1(3)

For the best programming practice, you should always explicitly specify both the lower and upper bound for the array, either in the Dim or a ReDim statement. So your first code should be

对于最佳编程实践,您应始终在Dim或ReDim语句中显式指定数组的下限和上限。所以你的第一个代码应该是

Sub CopyArray()
Dim Arr1(), Arr2(), Arr3()

Arr1 = Array(1, 2, 3, 4)

ReDim Arr2(UBound(Arr1))
ReDim Arr3(1 To 1, 0 To 3)

For i = LBound(Arr1) To UBound(Arr1)
    Arr2(i) = Arr1(i)
    Arr3(1, i) = Arr1(i)
Next i
    Debug.Print Join(Arr2, ",")
    Debug.Print Arr3(1, 0), Arr3(1, 1), Arr3(1, 2), Arr3(1, 3)
End Sub

Note: The function Debug.Print writes values to the Immediate Window to see the output of your code. To view this window select ViewImmediate Window from the menu or use keyboard shortcut CtrlG.

注意:Debug.Print函数将值写入立即窗口以查看代码的输出。要查看此窗口,请从菜单中选择View►ImmediateWindow或使用键盘快捷键CtrlG。

#4


1  

Here you have two things to keep in mind.

在这里你要记住两件事。

Firstly, you have to give a dimension to Arr2 via ReDim, for example you can do

首先,你必须通过ReDim为Arr2提供一个维度,例如你可以做到

ReDim Arr2(ubound(Arr1))

Before the For loop, since it has no dimension beforehand: Dim Arr2() declares an array that you have to change in dimension afterwards.

在For循环之前,因为它事先没有维度:Dim Arr2()声明一个数组,你必须在之后更改维度。

Secondly, since arrays start at 0 in VBA, you are having an issue populating the data from Arr1 that ranges from 0 to 3 and Arr3 that ranges from 1 to 4 (from the declaration ReDim Arr3(1 To m, 1 To n)). A good practice would have to use either all arrays starting at 0 or 1 but not both.

其次,由于数组在VBA中从0开始,因此存在填充Arr1中数据的问题,范围从0到3,Arr3的范围是1到4(来自声明ReDim Arr3(1到m,1到n)) 。一个好的做法是必须使用从0或1开始但不是两者的所有数组。

You can enforce the array dimension by using the Option Base 1 command (here look at the documention from MSDN ) and then all your arrays will start at 1.

您可以使用Option Base 1命令强制执行数组维度(此处查看MSDN中的文档),然后所有数组将从1开始。

As a consequence you can have it in two ways:

因此,您可以通过两种方式获得它:

1- Using Base 1

1-使用Base 1

Option Base 1

Sub CopyArray()

    Dim Arr1(), Arr2(), Arr3()

    Arr1 = Array(1, 2, 3, 4)

    'Since I assume you already have values for m and n, I give values for the code to work in our example
    m = 1
    n = 4

    ReDim Arr3(m, n)
    ReDim Arr2(ubound(Arr1))

    For i = LBound(Arr1) To UBound(Arr1)
        Arr2(i) = Arr1(i)
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

2- Keeping the 0 lower bound

2-保持0下限

Sub CopyArray()

    Dim Arr1(), Arr2(), Arr3()

    Arr1 = Array(1, 2, 3, 4)

    'Same as above
    m = 1
    n = 4

    ReDim Arr3(m, n)
    ReDim Arr2(ubound(Arr1))

    For i = LBound(Arr1) To UBound(Arr1)
        Arr2(i) = Arr1(i)
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

Notice that there is little difference between both codes, but one has all its arrays starting at 1 and not 0.

请注意,两个代码之间几乎没有差别,但是其中一个代码的所有数组都从1开始而不是0。

I'd recommend you to read also Lbound and Ubound for the lower and upper bound of arrays.

我建议您同时阅读Lbound和Ubound以了解数组的下限和上限。

#1


3  

Simplest way to copy one array to another in your case would be to declare the arrays as Variant. This way you will not have to loop

在您的情况下,将一个数组复制到另一个数组的最简单方法是将数组声明为Variant。这样你就不必循环了

Example of 1D array

一维数组的示例

Sub CopyArray()
    Dim x As Variant
    Dim y As Variant

    x = Array(1, 2, 3)

    y = x '<~~ Directly clone the array

    For i = LBound(y) To UBound(y)
        Debug.Print y(i)
    Next i
End Sub

Example of 2D Array

2D阵列的示例

Sub CopyArray()
    Dim x As Variant
    Dim y As Variant

    x = Array(1, 2, 3)

    y = Application.Transpose(x) '<~~ Transpose it

    For i = LBound(y) To UBound(y)
        Debug.Print y(i, 1)
    Next i
End Sub

#2


1  

First your i has to start at 0 because it is where arrays start. For example Arr1(1) = 2 and not 1 in your code. Secondly you have to Redim your second array so it has the same size as your first one. Here is a piece of code to help you out:

首先你的i必须从0开始,因为它是数组开始的地方。例如,Arr1(1)= 2而代码中不是1。其次,你必须Redim你的第二个数组,所以它与你的第二个数组大小相同。这是一段代码可以帮助您:

Dim Arr1(), Arr2()

Arr1 = Array(1, 2, 3, 4)
ReDim Arr2(0 To 3)

For i = 0 To 3
    Arr2(i) = Arr1(i)
Next i

#3


1  

The following code works fine:

以下代码工作正常:

Sub CopyArray()
    Dim Arr1(), Arr2(3)

    Arr1 = Array(1, 2, 3, 4)

    For i = 0 To 3
        Arr2(i) = Arr1(i)
    Next i

End Sub

Note that the declaration does not specify the number of elements in the array. Instead, it specifies the upper bound of the array. If your module does not contain an Option Base statement, the lower bound is assumed to be zero. So, the declaration above, Dim Arr2(3), is the same as

请注意,声明未指定数组中的元素数。相反,它指定数组的上限。如果您的模块不包含Option Base语句,则假定下限为零。因此,上面的声明,Dim Arr2(3),与之相同

Dim Arr2(0 To 3) As Variant

You may check that Arr1(1) is equal to 2, not 1 as you might think. Try to add the following line below the Next i to check each element of Arr1:

您可以检查Arr1(1)是否等于2,而不是您认为的1。尝试在Next i下面添加以下行来检查Arr1的每个元素:

Debug.Print Arr1(0), Arr1(1), Arr1(2), Arr1(3)

For the best programming practice, you should always explicitly specify both the lower and upper bound for the array, either in the Dim or a ReDim statement. So your first code should be

对于最佳编程实践,您应始终在Dim或ReDim语句中显式指定数组的下限和上限。所以你的第一个代码应该是

Sub CopyArray()
Dim Arr1(), Arr2(), Arr3()

Arr1 = Array(1, 2, 3, 4)

ReDim Arr2(UBound(Arr1))
ReDim Arr3(1 To 1, 0 To 3)

For i = LBound(Arr1) To UBound(Arr1)
    Arr2(i) = Arr1(i)
    Arr3(1, i) = Arr1(i)
Next i
    Debug.Print Join(Arr2, ",")
    Debug.Print Arr3(1, 0), Arr3(1, 1), Arr3(1, 2), Arr3(1, 3)
End Sub

Note: The function Debug.Print writes values to the Immediate Window to see the output of your code. To view this window select ViewImmediate Window from the menu or use keyboard shortcut CtrlG.

注意:Debug.Print函数将值写入立即窗口以查看代码的输出。要查看此窗口,请从菜单中选择View►ImmediateWindow或使用键盘快捷键CtrlG。

#4


1  

Here you have two things to keep in mind.

在这里你要记住两件事。

Firstly, you have to give a dimension to Arr2 via ReDim, for example you can do

首先,你必须通过ReDim为Arr2提供一个维度,例如你可以做到

ReDim Arr2(ubound(Arr1))

Before the For loop, since it has no dimension beforehand: Dim Arr2() declares an array that you have to change in dimension afterwards.

在For循环之前,因为它事先没有维度:Dim Arr2()声明一个数组,你必须在之后更改维度。

Secondly, since arrays start at 0 in VBA, you are having an issue populating the data from Arr1 that ranges from 0 to 3 and Arr3 that ranges from 1 to 4 (from the declaration ReDim Arr3(1 To m, 1 To n)). A good practice would have to use either all arrays starting at 0 or 1 but not both.

其次,由于数组在VBA中从0开始,因此存在填充Arr1中数据的问题,范围从0到3,Arr3的范围是1到4(来自声明ReDim Arr3(1到m,1到n)) 。一个好的做法是必须使用从0或1开始但不是两者的所有数组。

You can enforce the array dimension by using the Option Base 1 command (here look at the documention from MSDN ) and then all your arrays will start at 1.

您可以使用Option Base 1命令强制执行数组维度(此处查看MSDN中的文档),然后所有数组将从1开始。

As a consequence you can have it in two ways:

因此,您可以通过两种方式获得它:

1- Using Base 1

1-使用Base 1

Option Base 1

Sub CopyArray()

    Dim Arr1(), Arr2(), Arr3()

    Arr1 = Array(1, 2, 3, 4)

    'Since I assume you already have values for m and n, I give values for the code to work in our example
    m = 1
    n = 4

    ReDim Arr3(m, n)
    ReDim Arr2(ubound(Arr1))

    For i = LBound(Arr1) To UBound(Arr1)
        Arr2(i) = Arr1(i)
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

2- Keeping the 0 lower bound

2-保持0下限

Sub CopyArray()

    Dim Arr1(), Arr2(), Arr3()

    Arr1 = Array(1, 2, 3, 4)

    'Same as above
    m = 1
    n = 4

    ReDim Arr3(m, n)
    ReDim Arr2(ubound(Arr1))

    For i = LBound(Arr1) To UBound(Arr1)
        Arr2(i) = Arr1(i)
        Arr3(1, i) = Arr1(i)
    Next i

End Sub

Notice that there is little difference between both codes, but one has all its arrays starting at 1 and not 0.

请注意,两个代码之间几乎没有差别,但是其中一个代码的所有数组都从1开始而不是0。

I'd recommend you to read also Lbound and Ubound for the lower and upper bound of arrays.

我建议您同时阅读Lbound和Ubound以了解数组的下限和上限。