好吧,如果我了解您的要求,那么这个问题没有简单的 4 行答案。但这是一个简单的 90 行答案。您需要使枚举与您的两张纸“Final”和“Orig”上的列定义保持一致。您可能需要更改范围定义(工作表名称等)。您需要执行 VBE Menu/Tools/References 并检查“Microsoft Scripting Runtime”以获取字典对象。对不起,如果它看起来罗嗦,但它应该很容易维护。
Option Explicit
' This is the definition of the columns on "Final" sheet
Enum final_record
fr_partid
fr_descr
fr_vendorid
fr_po
fr_due
fr_quantdue
fr_status
fr_orig
fr_desired
fr_comment
fr_dayslate
fr_pri
fr_shoporder
fr_remarks
fr_end
End Enum
' This is the definition of the columns on "Orig" sheet
Enum orig_record
or_po
or_partid
or_vendorid
or_descr
or_comment
or_status
or_quant
or_balance
or_orig
or_requested
or_end
End Enum
Sub UpdateDescrAndShipDate()
' Update comments and required ship date if matches po/partid/description.
' Else add a new row.
Dim lRows As Long, lRow As Long, rFinal As Range, rOrig As Range, sKey As String
Dim lTarget As Long, lNew As Long
Dim dictTarget As New Scripting.Dictionary
' Get Final rows into dict by key
Set rFinal = Worksheets("Final").Range("A1")
lRows = rFinal.Offset(65000, 0).End(xlUp).Row - rFinal.Row
For lRow = 1 To lRows
sKey = rFinal.Offset(lRow, fr_po).Value & "|" & _
rFinal.Offset(lRow, fr_partid).Value & "|" & _
rFinal.Offset(lRow, fr_descr).Value
If Not dictTarget.Exists(sKey) Then
dictTarget.Add sKey, lRow
Else
MsgBox "Invalid duplicate key? " & sKey
End If
Next
lNew = lRows
' Run through Orig rows, and write to Orig based on key value
Set rOrig = Worksheets("Orig").Range("A1")
lRows = rOrig.Offset(65000, 0).End(xlUp).Row - rOrig.Row
For lRow = 1 To lRows
sKey = rOrig.Offset(lRow, or_po).Value & "|" & _
rOrig.Offset(lRow, or_partid).Value & "|" & _
rOrig.Offset(lRow, or_descr).Value
If dictTarget.Exists(sKey) Then
' update
lTarget = dictTarget(sKey)
rFinal.Offset(lTarget, fr_comment).Value = rOrig.Offset(lRow, or_comment).Value
rFinal.Offset(lTarget, fr_desired).Value = rOrig.Offset(lRow, or_requested).Value
Else
' new row
lNew = lNew + 1
rFinal.Offset(lNew, fr_partid).Value = rOrig.Offset(lRow, or_partid)
rFinal.Offset(lNew, fr_descr).Value = rOrig.Offset(lRow, or_descr)
rFinal.Offset(lNew, fr_vendorid).Value = rOrig.Offset(lRow, or_vendorid)
rFinal.Offset(lNew, fr_po).Value = rOrig.Offset(lRow, or_po)
rFinal.Offset(lNew, fr_due).Value = rOrig.Offset(lRow, or_balance)
rFinal.Offset(lNew, fr_quantdue).Value = rOrig.Offset(lRow, or_orig)
rFinal.Offset(lNew, fr_status).Value = rOrig.Offset(lRow, or_status)
rFinal.Offset(lNew, fr_orig).Value = ""
rFinal.Offset(lNew, fr_desired).Value = rOrig.Offset(lRow, or_requested)
rFinal.Offset(lNew, fr_comment).Value = rOrig.Offset(lRow, or_comment)
rFinal.Offset(lNew, fr_dayslate).Value = ""
rFinal.Offset(lNew, fr_pri).Value = ""
rFinal.Offset(lNew, fr_shoporder).Value = ""
rFinal.Offset(lNew, fr_remarks).Value = ""
End If
Next
End Sub