Matching two column and get values below the matched column

放肆的年华 提交于 2019-12-11 04:56:44

问题


I have one Pivot table in column A and one normal table in column D. Pivot table will be updated every month. I would like to match column A values with column D. Then if the values are not existed then the values in Column A must come under the last cell in column D. If the values are matched then no copy and paste.

I need output as shown in image. I used the formula below but it is not working. It is showing circular reference error. I used it on D13. How can i solve it. Help me

Is that possible in VBA

Excel formula which i used:

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

回答1:


Using circular reference function:

Go to File->Options->Formulas
Under Calculation options tick Enable iterative calculations
Set maximum of iterations to 1
Input following formula at first blank cell in Column D:

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

Drag it down, until you have all needed values.
Note you will still have blank cells, it is impossible to delete them within functions' scope.

Using VBA module:

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


来源:https://stackoverflow.com/questions/48747252/matching-two-column-and-get-values-below-the-matched-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!