1

2 つのデータ シリーズを日付と比較しようとしていますが、3 番目の列には両方のデータ シリーズに共通する日付のみを表示します (降順で並べ替えます)。私の友人は、動作するように見えるコードをまとめるのを手伝ってくれましたが、非常に長い一連のデータがある場合、結果を生成するのに時間がかかるようです。このコードを別の方法で記述して、計算を高速化する方法はありますか? (現在エクセル2010を使用しています。

D2 で入力してコピーする関数は次のとおりです。=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
4

2 に答える 2

1

これは、VBA がなくても実行できます。

列 C で COUNTIF を使用して、列 A と列 B の両方にのみ表示される日付を抽出します

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

次に、列 D で配列数式 (ここから) を使用して、空白を並べ替えて削除します。範囲を選択してから、Ctrl キー、Shift キー、Enter キーを押すことを忘れないでください。

=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))
于 2012-08-21T14:41:16.373 に答える
0

@Danのソリューションが機能する場合は、式のソリューションの方が通常はクールなので、それを使用してください:) 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 を反復処理し、Match 式が Date2 で成功/失敗するかどうかを確認します。成功 = 一致、つまり共通の日付。それらは別の列に出力されます。お役に立てれば!

于 2012-08-21T15:03:21.923 に答える