gpt4 book ai didi

vba - 从 sheet1 列 A、B、C、G、F、R、S、T 复制到列 A、B、C、D、E、F、G、H 中的表 2

转载 作者:行者123 更新时间:2023-12-04 21:08:11 26 4
gpt4 key购买 nike

VBA 中的 Excel 宏 2016。需要以不同的顺序将 8 个单独的列从一张纸复制到另一张纸。尝试过,但粘贴总是在同一列 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...?或任何其他顺便说一句...

此外,尝试了一个没有成功的 For 循环来避免复制/粘贴我的代码......类似于:

对于 (A,B,C,F,G,R,S,T) 中的 X

也没有运气...

非常感谢!

最佳答案

您没有告诉代码粘贴到哪里:Sheets("TMP").Paste .您只命名工作表而不命名列。

还可以使用循环,这样您就不需要继续重新输入相同的内容:

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

关于vba - 从 sheet1 列 A、B、C、G、F、R、S、T 复制到列 A、B、C、D、E、F、G、H 中的表 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51602689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com