2011-05-19 62 views
10

He escrito una macro que toma una matriz bidimensional y la "imprime" en celdas equivalentes en un libro de Excel.Función Excel VBA para imprimir una matriz en el libro

¿Hay una manera más elegante de hacer esto?

Sub PrintArray(Data, SheetName, StartRow, StartCol) 

    Dim Row As Integer 
    Dim Col As Integer 

    Row = StartRow 

    For i = LBound(Data, 1) To UBound(Data, 1) 
     Col = StartCol 
     For j = LBound(Data, 2) To UBound(Data, 2) 
      Sheets(SheetName).Cells(Row, Col).Value = Data(i, j) 
      Col = Col + 1 
     Next j 
      Row = Row + 1 
    Next i 

End Sub 


Sub Test() 

    Dim MyArray(1 To 3, 1 To 3) 
    MyArray(1, 1) = 24 
    MyArray(1, 2) = 21 
    MyArray(1, 3) = 253674 
    MyArray(2, 1) = "3/11/1999" 
    MyArray(2, 2) = 6.777777777 
    MyArray(2, 3) = "Test" 
    MyArray(3, 1) = 1345 
    MyArray(3, 2) = 42456 
    MyArray(3, 3) = 60 

    PrintArray MyArray, "Sheet1", 1, 1 

End Sub 

Respuesta

14

Del mismo tema como otras respuestas, hacer que sea sencillo

Sub PrintArray(Data As Variant, Cl As Range) 
    Cl.Resize(UBound(Data, 1), UBound(Data, 2)) = Data 
End Sub 


Sub Test() 
    Dim MyArray() As Variant 

    ReDim MyArray(1 To 3, 1 To 3) ' make it flexible 

    ' Fill array 
    ' ... 

    PrintArray MyArray, ActiveWorkbook.Worksheets("Sheet1").[A1] 
End Sub 
+0

Por el bien de Automatización OLE, asignando a 'Range' en realidad significa asignar a su' .Value'. –

3

Cree una matriz variante (la más fácil leyendo el rango equivalente en una variable variable).

A continuación, rellene la matriz y asigne la matriz directamente al rango.

Dim myArray As Variant 

myArray = Range("blahblah") 

Range("bingbing") = myArray 

La matriz variante terminará como una matriz 2-D.

0

una forma más elegante es asignar todo el conjunto a la vez:

Sub PrintArray(Data, SheetName, StartRow, StartCol) 

    Dim Rng As Range 

    With Sheets(SheetName) 
     Set Rng = .Range(.Cells(StartRow, StartCol), _ 
      .Cells(UBound(Data, 1) - LBound(Data, 1) + StartRow, _ 
      UBound(Data, 2) - LBound(Data, 2) + StartCol)) 
    End With 
    Rng.Value2 = Data 

End Sub 

Pero cuidado: sólo funciona hasta un tamaño de aproximadamente 8.000 células. Entonces Excel arroja un error extraño. El tamaño máximo no es fijo y difiere mucho de la instalación de Excel a la instalación de Excel.

0

Se puede definir un rango, el tamaño de la matriz y el uso que es propiedad del valor:

Sub PrintArray(Data, SheetName As String, intStartRow As Integer, intStartCol As Integer) 

    Dim oWorksheet As Worksheet 
    Dim rngCopyTo As Range 
    Set oWorksheet = ActiveWorkbook.Worksheets(SheetName) 

    ' size of array 
    Dim intEndRow As Integer 
    Dim intEndCol As Integer 
    intEndRow = UBound(Data, 1) 
    intEndCol = UBound(Data, 2) 

    Set rngCopyTo = oWorksheet.Range(oWorksheet.Cells(intStartRow, intStartCol), oWorksheet.Cells(intEndRow, intEndCol)) 
    rngCopyTo.Value = Data 

End Sub 
0

Mi versión probada

Sub PrintArray(RowPrint, ColPrint, ArrayName, WorkSheetName) 

Sheets(WorkSheetName).Range(Cells(RowPrint, ColPrint), _ 
Cells(RowPrint + UBound(ArrayName, 2) - 1, _ 
ColPrint + UBound(ArrayName, 1) - 1)) = _ 
WorksheetFunction.Transpose(ArrayName) 

End Sub 
1

Maneras más cortas: Simply Join & Elementos de la pantalla, es decir, utilizan la función join() para unirse a la matriz de elementos como una cadena, (sin tener que utilizar Loop o Hoja de trabajo estándar), por ejemplo:

MsgBox Join(arrayName, ",") 

'Above will display array elements as a single string separated by comma (a,b,c). 
Cuestiones relacionadas