Copy Paste VBA Range from one to another worksheet Loop and transpose the data

后端 未结 1 1670
离开以前
离开以前 2021-01-23 21:43

For A model in Excel I would like to copy and paste data from one sheet in a workbook to the other sheet in the workbook and transpose this data with the usage of a for next loo

1条回答
  •  青春惊慌失措
    2021-01-23 22:14

    I'm a little confused with what exactly you want to do but would this do?:

    First code: Not using transpose

    Sub Test()
    
    Dim X As Long, Y As Long, Z As Long
    
    Z = 2
    For X = 2 To 2420 Step 10
        For Y = 2 To 10
            Sheets("RME").Cells(Z, Y).Value = Sheets("CME").Cells(X + (Y - 1), 10).Value
        Next Y
        Z = Z + 1
    Next X
    
    End Sub
    

    Second code: Using transpose

    Sub Test()
    
    Dim X As Long, Z As Long
    Dim RNG1 As Range, RNG2 As Range
    
    Sheets("CME").Activate
    Z = 2
    For X = 2 To 2420 Step 10
        Set RNG1 = Sheets("CME").Range(Cells(X, 10), Cells(X + 9, 10))
        Set RNG2 = Sheets("RME").Cells(Z, 2)
        RNG1.Copy
        RNG2.PasteSpecial Transpose:=True
        Z = Z + 1
    Next X
    Sheets("RME").Activate
    
    End Sub
    

    Note: Copy/Pasting and transpose take much longer to execute on a larger database. To compare the two code examples above:

    • First code: 0,13 seconds
    • Second code: 5,5 seconds

    0 讨论(0)
提交回复
热议问题