在一个excel工作表(PO生成器)上查找与另一个Excel工作表上的记录匹配的记录。 (PO Log)Copy&将行粘贴到PO日志中

时间:2012-11-29 19:02:21

标签: excel vba

到目前为止,我拥有的代码......在PO生成器上获取原始订单并将其复制到PO日志,只要它们是新订单就可以。当我需要更新以前的订单时,我的问题就出现了。我希望它能找到原始订单并更新ref单元格。请帮助.... Excel 2003

Sub Get_Po()
'
' Get_Po Macro
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim dest As Range

Set ws1 = Sheets("PO GENERATOR")
Set ws2 = Sheets("PO LOG")
Set dest = ws2.Range("a1").End(xlDown).Offset(1)


Sheets("PO GENERATOR").Select
Selection.AutoFilter Field:=3, Criteria1:="<>"
ws1.Range("2:5135").Copy
dest.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Sheets("Type II Log").Select


End Sub 

1 个答案:

答案 0 :(得分:0)

以下是一些应该有效的代码。我已经测试了它,它可以满足您的需求。需要注意的几个要点是。您的PO生成器从哪个行和列开始,以及您的PO日志信息从哪个行和列开始。

    Dim a As String
Dim lastRow As String

lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
'   Variable for PO#
a = Sheets("PO GENERATOR").Range("A1").Value

Sheets("PO GENERATOR").Select
'   Adjust to whatever row your PO is located
Rows(1).Select
Selection.Copy
Sheets("PO LOG").Select

'   Modify the Columns(1) to whatever column number your PO# is located
'   Search For PO number
Dim cellrow As Integer
    On Error Resume Next
    rng1 = a
    strSearch = rng1
        Set aCell = Sheets("PO LOG").Columns(1).Find(What:=strSearch, LookIn:=xlValues, _
                                    LookAt:=xlWhole, SearchOrder:=xlByRows, _
                                    SearchDirection:=xlNext, MatchCase:=False, _
                                    SearchFormat:=False)

        MsgBox aCell & vbCrLf & aCell.Row
        cellrow = aCell.Row

'   Modify Range("A") to whatever column you PO is located
If cellrow = 0 Then
Range("A" & lastRow).Select
Selection.PasteSpecial
    Else:
        Rows(cellrow).EntireRow.Select
        Selection.PasteSpecial
End If

祝你好运!!!