Transposing Sets of Columns on Top of Each Other in Excel

前端 未结 2 1649
余生分开走
余生分开走 2021-01-17 08:30

So I have multiple sets of 3 columns. Each set is always in the same column order (\"SKU\", \"Sales\". \"Date\".)

I am wondering is there is a VBA script or other me

2条回答
  •  天涯浪人
    2021-01-17 09:12

    try to do this:

    Sub CopyColumns()
    
        Dim actualRow As Integer
        Dim actualColumn As Integer
    
        Dim rowFrom As Integer
        Dim myColumns As Integer
        Dim startColumn As Integer
    
        myColumns = 3 'the number of columns before start repeating (in your case is SKU, Sales, Date, so there are 3 columns)
        startColumn = 7 'the column where start de data. In your example is the Column G
    
        actualRow = 1
        actualColumn = 1
    
        rowFrom = 1
    
        Dim eoRows As Boolean
    
        eoRows = False
    
        While eoRows = False
    
            'verify if there's no more data
            If Cells(rowFrom, startColumn) = "" Then
                eoRows = True
            Else
                'verify if there's no more row
                While Cells(rowFrom, startColumn) <> ""
                    For i = startColumn To startColumn + myColumns - 1
                        Cells(actualRow, actualColumn) = Cells(rowFrom, i)
                        actualColumn = actualColumn + 1
                    Next
    
                    rowFrom = rowFrom + 1
                    actualRow = actualRow + 1
                    actualColumn = 1
    
                Wend
                rowFrom = 1
                startColumn = startColumn + myColumns
    
            End If
    
    
        Wend
    
    End Sub
    

提交回复
热议问题