copy from sheet1 cols A,B,C,G,F,R,S,T to sheet 2 in colums A,B,C,D,E,F,G,H

后端 未结 5 676
粉色の甜心
粉色の甜心 2021-01-16 02:25

Excel macro 2016 in VBA. Need to copy from 8 separated columns from one sheet to another, in different order. Tried but the Paste is done always in same column A...

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-16 03:06

    I think that this code is self explanatory and easy to modify.

    Sub Button1_Click()
        Application.Calculation = xlCalculationManual
        Application.ScreenUpdating = False
    
        Const CopyDataOnly As Boolean = False
        Dim c As Long
        Dim c1 As String, c2 As String
        Dim source As Range, target As Range
        With Sheets("Validation by rules")
            For c = 0 To 7
                c1 = Split("A,B,C,G,F,R,S,T", ",")(c)
                c2 = Split("A,B,C,D,E,F,G,H", ",")(c)
    
                Set source = .Range(.Cells(1, c1), .Cells(.Rows.Count, c1).End(xlUp))
                Set target = Sheets("TMP").Cells(1, c2)
                If CopyDataOnly Then
                    target.Resize(source.Rows.Count).Value = source.Value
                Else
                    source.Copy target
                End If
            Next
        End With
    
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    End Sub
    

提交回复
热议问题