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 646
粉色の甜心
粉色の甜心 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:01

    Use an array to collect, reshape then return the values.

    A,B,C,G,F,R,S,T to sheet TMP in columns A,B,C,D,E,F,G,H

    Sub Button1_Click()
    
        Dim i As Long, arr as variant
    
        with workSheets("Validation by rules")
    
            'collect
            i= .Cells(.Rows.Count, 1).End(xlUp).Row
            arr = .range(.cells(1,"A"), .cells(i, "T")).value
    
            'reshape part 1
            for i=lbound(arr, 1) to ubound(arr, 1)
                arr(i, 4) = arr(i, 7)
                arr(i, 5) = arr(i, 6)
                arr(i, 6) = arr(i, 18)
                arr(i, 7) = arr(i, 19)
                arr(i, 8) = arr(i, 20)
            next i
    
        end with
    
        'reshape part 2
        redim preserve arr(lbound(arr, 1) to ubound(arr, 1), lbound(arr, 2) to 8)
    
        'return
        workSheets("TMP").cells(1,1).resize(ubound(arr, 1), ubound(arr, 2)) = arr
    
    end sub
    

提交回复
热议问题