匹配两列并获取匹配列下方的值

Matching two column and get values below the matched column

我在 A 列中有一个 Pivot table,在 D 列中有一个正常 table。Pivot table 将每月更新一次。我想将 A 列的值与 D 列匹配。然后,如果值不存在,则 A 列中的值必须位于 D 列的最后一个单元格下方。如果值匹配,则无需复制和粘贴。

我需要如图所示的输出。我使用了下面的公式,但它不起作用。它显示循环引用错误。我在 D13 上使用它。 我该如何解决。帮帮我

VBA

有可能吗

Excel 我使用的公式:

=IF(ISNUMBER(MATCH(A2;$D:$D;0));"";A2)

使用循环引用函数:

转到文件->选项->公式
计算选项下勾选启用迭代计算
将最大迭代次数设置为 1
Column D 中的第一个空白单元格输入以下公式:

=IF(COUNTIF($D:INDIRECT(ADDRESS(COUNTA(D:D),4)),A2)=0,A2,"")

向下拖动它,直到获得所有需要的值。
请注意,您仍然会有空白单元格,无法在函数范围内删除它们。

使用 VBA 模块:

Option Explicit
Sub AddDict()
    Dim lRow As Long, iCell As Range
    Dim ClmnA As Range, ClmnD As Range
    Dim MySheet As Worksheet, iDict As Object
    '   Your worksheet, change "Test" accordingly
    Set MySheet = ThisWorkbook.Worksheets("Test")
    '   Create dictionary object
    Set iDict = CreateObject("Scripting.Dictionary")
    With MySheet
        '   Last row of the Column "A"
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        '   Range of Column "A" values starting from second row (without header)
        Set ClmnA = .Range("A2:A" & lRow)
        '   Last row of the Column "D"
        lRow = .Range("D" & .Rows.Count).End(xlUp).Row
        '   Range of Column "D" values starting from second row (without header)
        Set ClmnD = .Range("D2:D" & lRow)
    End With
    '   Loop through each cell in Column "D"
    For Each iCell In ClmnD.Cells
        '   Add cell value to dictionary (omitting duplicates)
        iDict(iCell.Value) = iCell.Value
    Next
    '   Loop through each cell in Column "A"
    For Each iCell In ClmnA.Cells
        '   Add cell value to dictionary (omitting duplicates)
        iDict(iCell.Value) = iCell.Value
    Next
    '   Populate Column "D" with dictionary items
    MySheet.Range("D2:D" & iDict.Count + 1) = Application.Transpose(iDict.Items)
End Sub