从sheet1 cols A,B,C,G,F,R,S,T复制到A,B,C,D,E,F,G,H列中的工作表2 [英] 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
问题描述
VBA中的Excel宏2016.需要从8张分离的列中以不同顺序从一张纸复制到另一张纸.尝试过,但粘贴始终在同一列A中完成.
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...
代码开头:
Sub Button1_Click()
Dim ultima_fila As Long
Dim rango, columna As String
Sheets("Validation by rules").Select
ultima_fila = Cells(Rows.Count, 1).End(xlUp).Row
' TableName
columna = "A"
rango = columna & "1:" & columna & CStr(ultima_fila)
MsgBox rango
range(rango).Copy
Sheets("TMP").Paste
'TableField
columna = "B"
rango = columna & "1:" & columna & CStr(ultima_fila)
MsgBox rango
range(rango).Copy
Sheets("TMP").Paste
但是,我不知道如何告诉宏将第二次粘贴到B ...中?或其他任何...
However, I don't know how to tell the macro to paste the second time into B...? or any other btw...
另外,尝试了一个不成功的For循环,以避免复制/粘贴我的代码……类似:
Also, tried a For loop with no success to avoid copy/paste my code... something like:
对于(A,B,C,F,G,R,S,T)中的X
For X in (A,B,C,F,G,R,S,T)
也没有运气...
非常感谢!
推荐答案
您没有告诉代码粘贴位置: Sheets("TMP").Paste
.您仅命名工作表而不命名列.
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
这篇关于从sheet1 cols A,B,C,G,F,R,S,T复制到A,B,C,D,E,F,G,H列中的工作表2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!