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 650
粉色の甜心
粉色の甜心 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:24

    You are not telling the code where to paste with: Sheets("TMP").Paste. You only name the sheet but not the column.

    Also use a loop so you do not need to keep retyping the same thing:

    Sub Button1_Click()
    
    Dim ultima_fila As Long
    Dim columnOrd As Variant
    columnOrd = Array("A", "B", "C", "G", "F", "R", "S", "T")
    
    With Sheets("Validation by rules")
        ultima_fila = .Cells(.Rows.Count, 1).End(xlUp).Row
    
        Dim i As Long
        For i = 1 To 8
            MsgBox .Range(.Cells(1, columnord(i - 1)), .Cells(ultima_fila, columnord(i - 1))).Address
            .Range(.Cells(1, columnord(i - 1)), .Cells(ultima_fila, columnord(i - 1))).Copy Destination:=Sheets("TMP").Cells(1, i)
        Next i
    End With
    
    End Sub
    

提交回复
热议问题