如何填补值的2维数组,然后把结果放到一个范围(How do I fill a 2 dimension

2019-10-18 18:24发布

这就有一个问题:“假设你的程序填补了一个大的二维阵列称为结果与价值观,你想它这些值转储到一个Excel范围。例如,如果结果为m乘n,你想程序转储值与m行和n列的范围。一种方法是在同一时间使用两个嵌套循环将一个元件转储数据到适当的小区“。

我有这么远:

    Dim MyArray(m, n) As Long
    Dim X as Long
    Dim Y as Long
        For X = 1 To m
        For Y = 1 To n 
            MyArray(X, Y) = Cells(X, Y).Value
        Next Y
        Next X

我真的需要一些帮助搞清楚了这一点,我完全失去了

Answer 1:

这种填充具有m行和n列的阵列,然后将其传送到所有在细胞的范围内的起始A1所述的ActiveSheet

Sub FillRangeFromArray()
Dim m As Long
Dim n As Long
Dim x As Long
Dim y As Long
Dim MyArray() As String

'set the row and column dimensions
m = 100
n = 5
'redimension the array now that you have m and n
ReDim MyArray(1 To m, 1 To n)
'whenever possible lbound and ubound (first to last element) 
'to loop through arrays, where
'MyArray,1 is the first (row) element and MyArray,2
'is the second (column) element
For x = LBound(MyArray, 1) To UBound(MyArray, 1)
    For y = LBound(MyArray, 2) To UBound(MyArray, 2)
        MyArray(x, y) = x & "-" & y
    Next y
Next x
'fill the range in one fell swoop
'Resize creates a range resized to
'm rows and n columns
ActiveSheet.Range("A1").Resize(m, n).Value = MyArray
End Sub


Answer 2:

假设你之前填写你的数据在其他地方,你可以只使用

Cells(X, Y).Value=MyArray(X, Y)


文章来源: How do I fill a 2 dimensional Array with Values and then put the results into a range