1

如何在不修改 B 列以前的数据的情况下将数据从 A 列(Sheet1)复制到 B 列(Sheet2)?先感谢您。

4

1 回答 1

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
于 2013-03-09T21:22:35.973 回答