VBA Transpose to Another target Sheet

时间:2016-07-11 21:41:58

标签: vba

In the pictures below, I have a source page that is formatted with a device and attributes. I would like to create a macro which copies that source data and pastes it in the format of the target sheet. Ultimately the target sheet would be empty and would populate only when running the macro.

Source page and target page

I am a newb at this. I've tried using the following code from another stack overflow question, but it doesn't do what I want.

Sub ColumnCopy()
Dim lastRow As Long
Dim lastRow As Long
Dim lastCol As Long
Dim colBase As Long
Dim tRow As Long
Dim source As String
Dim target As String

source = "source"       'Set your source sheet here
target = "target"       'Set the Target sheet name

tRow = 2                'Define the start row of the target sheet

'Get Last Row and Column
lastRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).Row
lastCol = Sheets(source).Cells(2, Columns.Count).End(xlToLeft).Column

tRow = 2
colBase = 2

Do While colBase < lastCol
    For iRow = 2 To lastRow
        Sheets(target).Cells(tRow, 1) = Sheets(source).Cells(1, tRow)
        Sheets(target).Cells(tRow, 2) = Sheets(source).Cells(2, tRow)
        Sheets(target).Cells(tRow, 3) = Sheets(source).Cells(3, tRow)
        Sheets(target).Cells(tRow, 4) = Sheets(source).Cells(4, tRow)
        Sheets(target).Cells(tRow, 5) = Sheets(source).Cells(5, tRow)
        Sheets(target).Cells(tRow, 6) = Sheets(source).Cells(6, tRow)
        tRow = tRow + 1
    Next iRow
        colBase = colBase + 1  'Add 4 to the Column Base.  This shifts the loop over to the next Row set.
Loop

End Sub

Thanks, MJ

0 个答案:

没有答案