Transposing is taking a rectangular block of data and rotating it so that columns become rows and vice versa. : Cell « Excel « VBA / Excel / Access / Word






Transposing is taking a rectangular block of data and rotating it so that columns become rows and vice versa.

 
Public Sub Transpose()
    Dim I As Integer
    Dim J As Integer
    Dim transArray() As Variant
    Dim numRows As Integer
    Dim numColumns As Integer
    Dim colIndex As Integer
    Dim rowIndex As Integer
    Dim inputRange As Range

    Set inputRange = ActiveWindow.Selection
    colIndex = inputRange.Column
    rowIndex = inputRange.Row
    numRows = inputRange.Rows.Count
    numColumns = inputRange.Columns.Count
    ReDim transArray(numRows - 1, numColumns - 1)
    For I = colIndex To numColumns + colIndex - 1
        For J = rowIndex To numRows + rowIndex - 1
            transArray(J - rowIndex, I - colIndex) = Cells(J, I).Value
        Next J
    Next I
    inputRange.ClearContents
    For I = colIndex To numRows + colIndex - 1
        For J = rowIndex To numColumns + rowIndex - 1
            Cells(J, I).Value = transArray(I - colIndex, J - rowIndex)
        Next J
    Next I
    Cells(rowIndex, colIndex).Select
End Sub

 








Related examples in the same category

1.Is a cell empty
2.Get the last cell
3.Magic Squares
4.Nest If statement in Do Loop While with comparison operators
5.Nest If statement in Do Loop Until for cells
6.Is active cell empty
7.Uses nested For/Next loops to count the total number of cells used in all open worksheets:
8.Selects cell G4
9.A better way to write to a range:Filling a range
10.Use arrays to fill ranges faster
11.Moving the pointer to the cell containing the greatest value