Excel convert columns to new rows

后端 未结 3 556
渐次进展
渐次进展 2020-12-22 14:49

I have a table that looks like this:

  |   A   |     B      |     C      |     D      |
  +-------+------------+------------+------------+
1 | Name  | Langua         


        
3条回答
  •  隐瞒了意图╮
    2020-12-22 15:42

    This will do the trick. It is also dynamic supports as many language columns as you want with as many languages per person. Assumes the data is formatted as per the example:

    Sub ShrinkTable()
        Dim maxRows As Double
        Dim maxCols As Integer
        Dim data As Variant
        maxRows = Cells(1, 1).End(xlDown).row
        maxCols = Cells(1, 1).End(xlToRight).Column
    
        data = Range(Cells(1, 1), Cells(maxRows, maxCols))
    
        Dim newSht As Worksheet
        Set newSht = Sheets.Add
    
        With newSht
    
            .Cells(1, 1).Value = "Name"
            .Cells(1, 2).Value = "Column"
    
            Dim writeRow As Double
            writeRow = 2
    
            Dim row As Double
            row = 2
            Dim col As Integer
    
            Do While True
    
                col = 2
                Do While True
                    If data(row, col) = "" Then Exit Do 'Skip Blanks
    
                    'Name
                    .Cells(writeRow, 1).Value = data(row, 1)
    
                    'Language
                    .Cells(writeRow, 2).Value = data(row, col)
    
                    writeRow = writeRow + 1
                    If col = maxCols Then Exit Do 'Exit clause
                    col = col + 1
                Loop
    
                If row = maxRows Then Exit Do 'exit cluase
                row = row + 1
            Loop
    
        End With
    End Sub
    

提交回复
热议问题