简体   繁体   中英

Excel 2010 Macro to transpose non adjacent cells

I'm currently transposing data from column to a row in a different spreadsheet one at a time. I began using the "record macro" function but I'm having trouble. I need the macro to copy data column by column and transpose it into a corresponding row, 15 rows apart. There are 100 entries per document. For example; P4 - P23 in document 1 needs to be transposed to M217 - AF217 in document 2. Q4 - Q23 needs to be transposed to M232 - AF 232, up to row 1501.

You can use this simple sub to transpose ranges.

Just give it the source and destination as a range:

Sub TransposeRange(SourceRange As Range, DestinationRange As Range)
    SourceRange.Copy
    DestinationRange.PasteSpecial Transpose:=True
End Sub


'Example:
Sub MoveRange()
    Call TransposeRange(Range("P4:P24"), Range("M232"))
End Sub

'Transpose columns 1-10 and put them row by row at column O
Sub MoveColumns()
    For x = 1 To 10
        Call TransposeRange(Intersect(UsedRange, Cells(1, x).EntireColumn), Cells(x, 15))
    Next x
End Sub

Result:

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM