【问题标题】:How Do I append items from list B to list A, that are in list B but not in list A?如何将列表 B 中的项目附加到列表 A,这些项目在列表 B 中但不在列表 A 中?
【发布时间】: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 中的最后一行并附加那些在匹配中返回错误的行。

标签: excel vba


【解决方案1】:

对于这种情况,我是字典的忠实粉丝。利用它们独特的键和 Exists 方法,比不断循环检查您想要检查的每个项目更容易。只需确保添加对项目的引用,以便可以从 Windows 访问 Scripting.Dictionary。

下面的代码就像一个框架,可以让你朝着正确的方向前进。我确实做了一些假设,我试图在代码中指出,比如列表 A 是 A 列,我没有考虑标题。

Option Explicit

Public Sub CreateDictToCompare()

    Dim LastRow As Long
    Dim i As Long
    Dim Dict As Scripting.Dictionary

    'Get's the last row of column A
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    End With

    Set Dict = New Scripting.Dictionary

    For i = 1 To LastRow
        'Assuming List A is unique values and in Column A
        Dict.Add Key:=ActiveSheet.Range(i, 1).value, Item:=vbNullString
    Next i

    'Gets the last row of column B
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
    End With

    For i = 1 To LastRow
            'Assuming the Values you want to compare are in column B
        If Not Dict.Exists(ActiveSheet.Range(i, 2).value) Then
            'You will only get here if the Value is not in list A.
            'You can use this space to append this value to list B
        End If
    Next i

End Sub

希望这能让你朝着正确的方向前进,祝你好运!

【讨论】:

  • 非常感谢!这真的很好解释。我今天会试试这个,让你知道它是怎么回事:)
猜你喜欢
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多