如果在列中find重复的单元格值,则返回值

我需要跟踪数据表中的人来确定人从哪个位置移动到哪个位置。

如果一个人在列J中多出现一次,这意味着该人已经改变了位置,并且位置值在列L中。为此,我具有以下代码:

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

问题是如果人改变了两次以上的位置。 在O栏AA栏中,我有一年中确定人的位置的月份。

我怎样才能修改这个代码来做到以上几点:

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

例

这是用户定义的函数 (aka UDF)来完成任务。

 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 

位置日历单元格只需要由第一行定义。 每一个都有其高度(即 )重新定义,以保持与名单的一致性。

连接位置

在AB2中(如上)公式是,

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

根据需要填写。