根据两个date序列中的常见date生成新的date序列

我试图比较两个数据系列与date和第三列只显示两个数据序列(按降序排列)中通用的date。 我的一个朋友帮助我编写了一些似乎可行的代码,但是当我有相当长的一系列数据时,似乎需要很长时间才能得出结果。 有没有办法来写这个代码不同,可能会得到更快的计算? (我目前正在使用Excel 2010。

我在D2上input的函数是: =next_duplicate(A2:$A$535,B2:$B$535,D1:$D$1)

在这里输入图像说明

 Function next_duplicate(list1, list2, excluded) For Each c In list1 If WorksheetFunction.CountIf(excluded, c) = 0 Then If WorksheetFunction.CountIf(list2, c) > 0 Then next_duplicate = c Exit For End If End If Next c If next_duplicate = 0 Then next_duplicate = "N/A" End If End Function 

你可以做到这一点没有VBA。

在列C中使用COUNTIF来提取只出现在列A和B中的date

 =IF(COUNTIF($B$2:$B$7,"="&A2) > 0, A2, 0) 

然后在D列使用数组公式(从这里 )sorting和删除空白。 不要忘记select范围,然后按控制,移动并进入。

 =INDEX(C2:C7, MATCH(LARGE(IF(ISBLANK(C2:C7), "", IF(ISNUMBER(C2:C7), COUNTIF(C2:C7, "<"&C2:C7), COUNTIF(C2:C7, "<"&C2:C7)+SUM(IF(ISNUMBER(C2:C7), 1, 0))+1)), ROW()-ROW($D$2)+1), IF(ISBLANK(C2:C7), "", IF(ISNUMBER(C2:C7), COUNTIF(C2:C7, "<"&C2:C7), COUNTIF(C2:C7, "<"&C2:C7)+SUM(IF(ISNUMBER(C2:C7), 1, 0))+1)), 0)) 

如果@ Dan的解决scheme能够正常工作,那就去配方解决scheme通常更酷:)如果您需要使用VBA,您可以试试这个:

 Sub Common() Dim Date1 As Range Dim Date2 As Range Dim CommonDates() As Variant Dim UniqueDates As New Collection Set Date1 = Range("A2:A6") Set Date2 = Range("B2:B6") ' Change the max array size to equal the length of Date1 ' This is arbitrary and could be more efficient, for sure :) ReDim CommonDates(Date1.Count) ' Set a counter that will increment with matches i = 0 ' Since a match needs to be in both, iterate through Date1 and check ' if the Match function returns a True value when checking Date2. ' If so, add that value to the CommonDates array and increment the counter. For Each DateVal In Date1 If IsError(Application.Match(DateVal, Date2, 0)) = False Then CommonDates(i) = DateVal.Value i = i + 1 End If Next ' Filter out dupes (in case that is possible - if not, this can be removed ' and the bottom part reworked On Error Resume Next For Each Value In CommonDates UniqueDates.Add Value, CStr(Value) Next Value ' Now go to the first cell in your Common Dates range (I'm using C2) and ' print out all of the results Range("C2").Activate For j = 1 To UniqueDates.Count ActiveCell.Value = UniqueDates(j) ActiveCell.Offset(1).Activate Next j ' Back to the beginning Range("C2").Activate ' Use this if you don't need to filter dupes 'For Each r In CommonDates ' ActiveCell.Value = r ' ActiveCell.Offset(1).Activate 'Next End Sub 

它基本上迭代Date1,并检查匹配公式成功/失败Date2。 成功=匹配,这意味着一个普通的date。 那些被打印到另一列。 希望这可以帮助!