编辑Excel宏VBA以使其填充C列和右侧,而不是列A.

时间:2013-01-30 23:35:05

标签: excel vba edit

我目前正在使用下面的宏来表示将数据从一张纸移动到另一张纸。只要行为空,它就会设置为从第2行填充。我不希望它已经包含第2列和第2列中的数据。我尝试过很多东西,但运气不好。我是新手,并“修复”别人的宏。

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

1 个答案:

答案 0 :(得分: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