1

我目前正在使用下面的宏进行 excel 将数据从一张表移动到另一张表。只要行是空的,它就设置为从第 2 行向下填充。我不想让它已经包含第 2 列和第 3 列中的数据。我已经尝试了很多东西,但运气不佳。我对此并不陌生,并且“修复”了别人的宏。

Sub MergeSheets()

Sheets("New").Activate
LastRowNew = Application.WorksheetFunction.CountA(Columns(1))
For i = 2 To LastRowNew
    OrderNumber = Cells(i, 3)
    Sheets("PRIOrders").Activate
    LastRowPRI = Application.WorksheetFunction.CountA(Columns(1))
    For j = 2 To LastRowPRI
        If Cells(j, 3) = OrderNumber Then
            Exit For
        ElseIf j = LastRowPRI Then
            Sheets("New").Rows(i).Copy Destination:=Sheets("PRIOrders").Rows(LastRowPRI + 1)
            Sheets("PRIOrders").Rows(2).Copy
            Sheets("PRIOrders").PasteSpecial xlPasteFormats
        End If
    Next
    Sheets("New").Activate
Next
4

1 回答 1

0
Sub MergeSheets()

Dim shtNew As Worksheet, shtOrders As Worksheet
Dim rngOrder As Range, rngNewOrders As Range
Dim f As Range, lastRow As Long

    Set shtNew = ActiveWorkbook.Sheets("New")

    Set rngNewOrders = shtNew.Range(shtNew.Range("C2"), _
                       shtNew.Cells(Rows.Count, 3).End(xlUp))

    Set shtOrders = ActiveWorkbook.Sheets("PRIOrders")

    For Each rngOrder In rngNewOrders.Cells

        Set f = shtOrders.Columns(3).Find(Trim(rngOrder.Value), , xlValues, xlWhole)
        If f Is Nothing Then
            'find the last occupied row in Col B or C
            lastRow = Application.Max(shtOrders.Cells(Rows.Count, 2).End(xlUp).Row, _
                                      shtOrders.Cells(Rows.Count, 3).End(xlUp).Row)

            rngOrder.EntireRow.Copy shtOrders.Cells(lastRow + 1, 1)

        End If

    Next rngOrder

End Sub
于 2013-01-31T00:26:27.927 回答