【发布时间】:2019-08-25 05:33:25
【问题描述】:
我有以下问题: 我有两个列表,A 和 B。列表 B 会定期更新,并且可能包含新值。列表 A 保持不变。如何将列表 B 中当前不在列表 A 中的新项目添加到列表 A?
我可能会做一个 vlookup 并返回缺失值,但我不知道如何将这些缺失的项目附加到列表 A。
是否有一个简单的 VBA 代码来执行此操作?
*****更新:******
因此,使用下面的答案,我尝试编写宏脚本,但是当我尝试将项目添加到字典时出现运行时错误提示对象未定义?:
Option Explicit
Sub AppendProfitCentres()
Dim LastRowRecon As Long
Dim LastRowSAP As Long
Dim Dict As Object
Dim MissingPC As Long
Dim i As Integer
Worksheets("Recon").Range("K6").Select
Worksheets("Recon").Range("K6", Selection.End(xlDown)).Select
LastRowRecon = Cells(Rows.Count, 11).End(xlUp).Row
Cells(LastRowRecon, 11).Select
'
''create dictionary to hold profit centres
'
'
Set Dict = CreateObject("Scripting.Dictionary")
Worksheets("Recon").Range("K6").Select
For i = 6 To LastRowRecon
'
Dict.Add Key:=Worksheets("Recon").Range(i, 11).Value, Item:=vbNullString
Next i
'check SAP and TCM profit centres against Dictionary PC
Worksheets("SAP Data").Range("A7").Select
Worksheets("SAP Data").Range("A7", Selection.End(xlDown)).Select
LastRowSAP = Cells(Rows.Count, 1).End(xlUp).Row
For i = 7 To LastRowSAP
If Not PC.Exists(Worksheets("SAP Data").Range(i, 1).Value) Then
'if item doesnt exist, append to profit centres in recon tab
MissingPC = Empty
MissingPC = Worksheets("SAP Data").Range(i, 1).Value
Cells(LastRowRecon, 11).Select
ActiveCell.Offset(1).EntireRow.Insert
ActiveCell.Value = MissingPC
End If
Next i
End Sub
【问题讨论】:
-
到目前为止你尝试过什么代码? Stack Overflow 不是代码编写服务。
-
很遗憾目前还没有编写任何代码。我是 VBA 的新手,所以仍然想了解我可以使用哪些 VBA 函数来执行此操作?
-
您将需要一个 For 循环和 Application.Match() 来查找它是否已经存在,然后只需找到 A 中的最后一行并附加那些在匹配中返回错误的行。