繁体   English   中英

Excel VBA将列复制到另一列

[英]Excel VBA copy column to another column

这是代码:

Public Sub mysub()
Dim colA As Integer, colB As Integer
Dim rowA As Integer, rowB As Integer
colA = 3
colB = 19
rowA = 7
rowB = 7
lastA = Cells(Rows.Count, colA).End(xlUp).Row
For x = rowA To lastA
    Data = Cells(x, colA)
    Cells(rowB, colB) = Data
    rowB = rowB + 1
Next x

End Sub

它工作正常,但会不断重写colB,我如何让它粘贴在另一列上? 我想尝试End(xlLeft).column但不能让它工作

据我了解,您正在尝试将数据粘贴到特定行中的第一个空列中。 如果是这样,那么您可以使用以下代码:

Public Sub mysub()
Dim colA As Integer, colB As Integer
Dim rowA As Integer, rowB As Integer
colA = 3
rowA = 7
rowB = 7
lastA = Cells(Rows.Count, colA).End(xlUp).Row
For x = rowA To lastA
    Data = Cells(x, colA)

    'check for first empty column in the x row
    colB = Cells(x, Columns.Count).End(xlToLeft).Column + 1

    Cells(rowB, colB) = Data
    rowB = rowB + 1
Next x

结束子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM