Excel VBA复制列到另一列

时间:2014-07-15 08:09:15

标签: excel vba excel-vba

这是代码:

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但是不能让它工作

1 个答案:

答案 0 :(得分:0)

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

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

End Sub