Excel VBA - 如何使用单个命令从3D矩阵复制2D矩阵内容?

时间:2023-01-18 12:47:28

I have a 3D matrix, which contains something I would like to write in a range of cells using the command:

我有一个3D矩阵,其中包含我想在一系列单元格中使用以下命令编写的内容:

Worksheet.Range(Worksheet.Cells(RowStart, ColumnStart), Worksheet.Cells(RowEnd, ColumnEnd)).Value = MyData

The problem is that MyData's data is actually described by index = 2 of the first dimension of a 3D matrix.

问题是MyData的数据实际上是由3D矩阵的第一维的index = 2描述的。

Since I don't see the possibility to use Application.WorksheetFunction.Index here, I would like to copy the content of the whole specific index of the 3D matrix inside a 2D one.

由于我没有看到在这里使用Application.WorksheetFunction.Index的可能性,我想在2D内部复制3D矩阵的整个特定索引的内容。

Is this possible without having to resort to a For loop (or writing a subroutine that does it)?

这是可能的,而不必求助于For循环(或编写一个子程序)?

1 个解决方案

#1


2  

I've made that custom Sub a while ago to display part of a 3D array :

我刚刚制作了那个自定义Sub来显示3D数组的一部分:

Public Sub Print2D_of_3D_Array(ByVal ArrayT As Variant, _
                ByVal FixedDim As Integer, _
                ByVal FixedDimValue As Double, _
                ByRef Sheet_to_PrintOn As Worksheet, _
                Optional ByVal DeleteSheet As Boolean = False)


If DeleteSheet Then Set Sheet_to_PrintOn = DeleteAndAddSheet(Sheet_to_PrintOn)

Select Case FixedDim
    Case Is = 1
        For i = LBound(ArrayT, 2) To UBound(ArrayT, 2)
            For j = LBound(ArrayT, 3) To UBound(ArrayT, 3)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(FixedDimValue, i, j)
            Next j
        Next i
    Case Is = 2
        For i = LBound(ArrayT, 1) To UBound(ArrayT, 1)
            For j = LBound(ArrayT, 3) To UBound(ArrayT, 3)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(i, FixedDimValue, j)
            Next j
        Next i
    Case Is = 3
        For i = LBound(ArrayT, 1) To UBound(ArrayT, 1)
            For j = LBound(ArrayT, 2) To UBound(ArrayT, 2)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(i, j, FixedDimValue)
            Next j
        Next i
    Case Else
        MsgBox "error"
End Select

End Sub

So, you'd have to use it like this :

所以,你必须像这样使用它:

Call Print2D_of_3D_Array(MyArray, 1, 2, WorksheetVariable, False)

#1


2  

I've made that custom Sub a while ago to display part of a 3D array :

我刚刚制作了那个自定义Sub来显示3D数组的一部分:

Public Sub Print2D_of_3D_Array(ByVal ArrayT As Variant, _
                ByVal FixedDim As Integer, _
                ByVal FixedDimValue As Double, _
                ByRef Sheet_to_PrintOn As Worksheet, _
                Optional ByVal DeleteSheet As Boolean = False)


If DeleteSheet Then Set Sheet_to_PrintOn = DeleteAndAddSheet(Sheet_to_PrintOn)

Select Case FixedDim
    Case Is = 1
        For i = LBound(ArrayT, 2) To UBound(ArrayT, 2)
            For j = LBound(ArrayT, 3) To UBound(ArrayT, 3)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(FixedDimValue, i, j)
            Next j
        Next i
    Case Is = 2
        For i = LBound(ArrayT, 1) To UBound(ArrayT, 1)
            For j = LBound(ArrayT, 3) To UBound(ArrayT, 3)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(i, FixedDimValue, j)
            Next j
        Next i
    Case Is = 3
        For i = LBound(ArrayT, 1) To UBound(ArrayT, 1)
            For j = LBound(ArrayT, 2) To UBound(ArrayT, 2)
              Sheet_to_PrintOn.Cells(i + 1, j + 1) = ArrayT(i, j, FixedDimValue)
            Next j
        Next i
    Case Else
        MsgBox "error"
End Select

End Sub

So, you'd have to use it like this :

所以,你必须像这样使用它:

Call Print2D_of_3D_Array(MyArray, 1, 2, WorksheetVariable, False)