IF duplicate cell value found in column then return value

若如初见. 提交于 2019-12-11 03:57:49

问题


I need to track a person in a data sheet to determine from which location to which location the person moved.

If a person appears more then one time in Column J that means the person has changed the location and the location value is in Column L. For this I have the following code:

=IF(J18=J19;IF(COUNTIF(J:J;J18)>1; "From "&L18 &" to "& IF(J18=J19;L19;"");"");"")

The problem is if the person changes the location more than two times. In Column O to Column AA I have the months of the year which determines the location of the person.

How can I modify this code to do the above:

=IF(J18=J19;IF(COUNTIF(J:J;J18)>1; "From "&L18 &" to "& IF(J18=J19;L19;"");"");"")


回答1:


Here is a User Defined Function (aka UDF) to accomplish the task.

Function my_Travels(nm As Range, loc As Range, cal As Range)
    Dim n As Long, cnt As Long, v As Long, vLOCs As Variant, vTMPs As Variant
    Dim iLOC As Long, sTMP As String

    my_Travels = vbNullString   '"no travels"
    cnt = Application.CountIf(nm.EntireColumn, nm(1))

    If Application.CountIf(nm, nm(1)) = cnt And cnt > 1 Then
        Set loc = loc.Rows(1).Resize(nm.Rows.Count, loc.Columns.Count)
        Set cal = cal.Rows(1).Resize(nm.Rows.Count, cal.Columns.Count)

        'seed the array
        ReDim vLOCs(1 To cnt, 1 To cnt)
        For v = LBound(vLOCs, 1) To UBound(vLOCs, 1)
            vLOCs(v, 1) = cal.Columns.Count + 1
            vLOCs(v, 2) = cal.Columns.Count + 1
        Next v

        'collect the values into the array
        For n = 1 To nm.Rows.Count
            If nm.Cells(n, 1).Value2 = nm.Cells(1, 1).Value2 Then
                iLOC = Application.Match(1, Application.Index(cal, n, 0), 0)
                For v = LBound(vLOCs, 1) To UBound(vLOCs, 1)
                    If vLOCs(v, 1) = cal.Columns.Count + 1 Then
                        vLOCs(v, 1) = iLOC
                        vLOCs(v, 2) = n
                        Exit For
                    End If
                Next v
            End If
        Next n

        'sort the values in the array
        For v = LBound(vLOCs, 1) To (UBound(vLOCs, 1) - 1)
            For n = (v + 1) To UBound(vLOCs, 1)
                If vLOCs(v, 1) > vLOCs(n, 1) Then
                    vTMPs = Array(vLOCs(v, 1), vLOCs(v, 2))
                    vLOCs(v, 1) = vLOCs(n, 1)
                    vLOCs(v, 2) = vLOCs(n, 2)
                    vLOCs(n, 1) = vTMPs(0)
                    vLOCs(n, 2) = vTMPs(1)
                    Exit For
                End If
            Next n
        Next v

        'concatenate the locations from the array
        For v = LBound(vLOCs) To (UBound(vLOCs) - 1)
            sTMP = sTMP & "From " & loc.Cells(vLOCs(v, 2), 1) & " to " & loc.Cells(vLOCs(v + 1, 2), 1) & "; "
        Next v

        'truncate the string and return it
        sTMP = Left(sTMP, Len(sTMP) - 2)
        my_Travels = sTMP

    End If

End Function

The Locations and the Calendar cells only need to be defined by the first row. Each has its height (i.e. rows) redefined to maintain consistency with the list of names.

    

In AB2 (as above) the formula is,

=my_Travels(J2:J$8, L2, O2:AA2)

Fill down as necessary.



来源:https://stackoverflow.com/questions/30492388/if-duplicate-cell-value-found-in-column-then-return-value

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