我在A列中有一个Pivot表,在D列中有一个普通表。每个月都会更新数据透视表。我想将列A值与列D匹配。然后,如果值不存在,则列A中的值必须位于列D中的最后一个单元格下。如果值匹配则不复制和粘贴。
我需要输出,如图所示。我使用下面的公式,但它不起作用。它显示循环引用错误。我在D13上用它。 我该如何解决呢。帮帮我
这可能在VBA中
我使用的Excel公式:
=IF(ISNUMBER(MATCH(A2;$D:$D;0));"";A2)
答案 0 :(得分:2)
转到文件 - >选项 - >公式
在计算选项下勾选启用迭代计算
将迭代次数设置为1
在Column D
的第一个空白单元格中输入以下公式:
=IF(COUNTIF($D$2:INDIRECT(ADDRESS(COUNTA(D:D),4)),A2)=0,A2,"")
将其向下拖动,直到您拥有所有需要的值 请注意,您仍然会有空白单元格,无法在函数范围内删除它们。
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