Transpose a single column to multiple rows

后端 未结 2 1917
有刺的猬
有刺的猬 2021-01-29 08:58

I have one column of data with approximately 800 records. Each 18 cells is one record. I need a script that takes every 18 cells and transposes them to a row dynamically, or ev

2条回答
  •  被撕碎了的回忆
    2021-01-29 09:29

    you can use this VBA code to achieve this .

        Public Sub TransposePaste()
       'Value we want to step by....
        Dim stepVal As Long
        stepVal = 18
    'Declare start row variable (-1)
     Dim row As Long
     row = 0
    
    'Declare the column the data is coming from
    Dim col As Long
    col = 1
    
    'Declare and set worksheet
    Dim ws As Worksheet
    Set ws = Sheet1
    
    'end row of the data
    Dim endRow As Long
    
    
    endRow = ws.Cells(ws.Rows.Count, col).End(xlUp).row
    
    'cycle to end of data end of data...
    For i = 1 To endRow Step stepVal
    
    'increment row each time
    row = row + 1
    
        'cycle through the columns outputting the data
        For j = 1 To stepVal
    
        Sheet1.Cells(row, j).Value = Sheet1.Cells(i + j - 1, col).Value
    
        Next j
    Next i
    

    End Sub

提交回复
热议问题