从B列的先前数据下面的A列(Sheet1)复制到B列(Sheet2)

时间:2013-03-09 20:31:12

标签: vba excel-vba excel-2010 excel

如何将数据从A列(Sheet1)复制到B列(Sheet2)而不修改colunm B上的先前数据?先谢谢你。

1 个答案:

答案 0 :(得分:1)

这是一个通用答案,它不会替换单元格,但会查找下一个单元格。我试图发表意见,这有助于理解它。

Sub CopySheet1toSheet2()

    Dim CurrRow As Integer
    Dim CurrCol As Integer
    Dim TargetRow As Integer
    Dim TargetCol As Integer

    Dim Sheet1 As String
    Dim Sheet2 As String

    Sheet1 = "Sheet1" ' name to your source sheet name
    Sheet2 = "Sheet2" ' change to your target sheet name
    CurrRow = 1
    CurrCol = 1 ' This is column A

    TargetRow = 1
    TargetCol = 2 ' This is column B

    ' Cycle through all rows in Col A until empty field reached
    While Sheets(Sheet1).Cells(CurrRow, CurrCol) <> ""

        ' See if there's a value in column B in the same row - if so, go to the next row
        While Sheets(Sheet2).Cells(TargetRow, TargetCol) <> ""
            TargetRow = TargetRow + 1
        Wend

        ' Copy the value from the source to the target
        Sheets(Sheet2).Cells(TargetRow, TargetCol) = Sheets(Sheet1).Cells(CurrRow, CurrCol)

        ' Move to the next source and target rows
        CurrRow = CurrRow + 1
        TargetRow = TargetRow + 1

    Wend

End Sub