0

コードがタスクを実行する速度を上げるために解決すべき次の問題があります。

レンタカーの名前と2つの日付(FromとTo)のテーブルがあります。範囲(たとえば10k行)を調べて、重複するすべての日付を強調表示する必要があります。

からまでレンタカーはありません

1 ABC 01 Jan 12 12 Jan 12
2 ABC 14 Jan 12 15 Jan 12
3 ABC 25 Jan 12 02 Feb 12
4 DEF 01 Jan 12 12 Jan 12
5 DEF 12 Jan 12 02 Feb 12
6 DEF 14 Jan 12 15 Jan 12

レンタカーのDEFの場合、重複する日があります。実際には、ユーザーがすばやく識別して修正できるように、強調表示できるようにする必要がある二重カウントがあります。

これは私が開発したコードです。問題は、範囲が10k行の場合、非常に遅いことです。

Office /Excel2010でWindows7Ultimateを使用しています

    Function CheckOverlap(StartLine, EndLine, StartColumn)

Dim i As Integer, y As Integer
Dim DateToCompare
Dim HireCar
Dim Count As Integer
Dim Msg, Style, Title, Response

'Check StartDate Column
For i = StartLine To EndLine

    DateToCompare = Cells(i, StartColumn)
    HireCar = Cells(i, 2)
    For y = StartLine To EndLine
        'If we are at the same line with DateToCompare cell then we should not perform any check
        If i <> y Then    
            If DateToCompare >= Cells(y, StartColumn) And DateToCompare <= Cells(y, StartColumn + 1) And HireCar = Cells(y, 2) Then
                'We should highlight both cells that contain overlapping dates
                ActiveSheet.Cells(i, StartColumn).Interior.Color = 5296274
                ActiveSheet.Cells(y, StartColumn).Interior.Color = 5296274
            End If
        End If
    Next y
Next i

HireCar = 0

'Check EndDate Column
For i = StartLine To EndLine

    DateToCompare = Cells(i, StartColumn + 1)
    HireCar = Cells(i, StartColumn - 1)
    For y = StartLine To EndLine
        'If we are at the same line with DateToCompare cell then we should not perform any check
        If i <> y Then    
            If DateToCompare >= Cells(y, StartColumn) And DateToCompare <= Cells(y, StartColumn + 1) And HireCar = Cells(y, StartColumn - 1) Then
                'We should highlight both cells that contain overlapping dates
                ActiveSheet.Cells(i, StartColumn + 1).Interior.Color = 5296274
                ActiveSheet.Cells(y, StartColumn + 1).Interior.Color = 5296274
            End If
        End If
    Next y
Next i


'Last check: If the starting and ending date are the same
For i = StartLine To EndLine
    If Cells(i, StartColumn) - Cells(i, StartColumn + 1) = 0 Then
        ActiveSheet.Cells(i, StartColumn).Interior.Color = 5296274
        ActiveSheet.Cells(i, StartColumn + 1).Interior.Color = 5296274
    End If
Next i

' If there are no Overlap Days in Database skip filtering
' StartDate and EndDate Column
' Count Cells with Interior.Color = 5296274 (Green Colour)
Count = 0

For i = StartLine To EndLine
    If Cells(i, StartColumn).Interior.Color = 5296274 Then
        Count = Count + 1
    End If
Next i

' Msg if Database has no Overlap Days
Msg = "Validation check completed. There are 'NO' Overlap Days"
Style = vbOKOnly
Title = "Cash Flow"

' Require on Error Resume Next in case Database is NOT filtered
On Error Resume Next
If Count = 0 Then
    ActiveSheet.ShowAllData
    Response = MsgBox(Msg, Style, Title)
    Exit Function
Else
    Call Filter_Colour
End If

MsgBox "Any Green highlights indicate Overlap Days"

終了機能

4

2 に答える 2

0

最も速い方法は、テーブルを並べ替えることです (最初の順序: 車、2 番目の順序: 開始日)。

次に、各行について: 上の行が同じ車で、上の行の終了日が現在の行の開始日よりも大きい場合、衝突が発生します。

これらの手順は、VBA または Excel-Formulas を使用して実行できます。

于 2013-01-25T05:56:42.073 に答える
0

後の行に重複がある場合に空白を表示する簡単なアルゴリズムを次に示します。これを実行するには、質問に示されているサンプルに従って CAR 列がソートされていることが厳密に想定されています。

Option Explicit

'-- assuming the CAR names column is sorted
'-- so each car block in one place
'-- run on button click event

Sub FindOverlaps()
Dim i As Integer, j As Integer
Dim vInput As Variant
Dim rng As Range

Set rng = Sheets(2).Range("B2:E7")
vInput = WorksheetFunction.Transpose(WorksheetFunction.Transpose(rng))

For i = LBound(vInput) To UBound(vInput) - 1
    For j = LBound(vInput) + 1 To UBound(vInput)
        If vInput(i, 2) = vInput(j, 2) Then
            If vInput(i, 4) = vInput(j, 3) Then
                vInput(j, 3) = ""
                vInput(j, 4) = ""
            End If
        End If
    Next j
Next i

rng.Offset(0, 6).Resize(UBound(vInput), UBound(Application.Transpose(vInput))) = vInput

End Sub

出力:

ここに画像の説明を入力


OPのコメントに従って編集

  1. ソートされたデータを入力データごとに同じ範囲offset(0,4)に転置するため、次を削除します。
  2. conditiona formatting指定された範囲内の null である任意の行を強調表示するために追加します。(そうしないと、空のセルがあるシート全体が色付けされます)

コードの変更:

rng.Offset(0, 6).FormatConditions.Delete
rng.Offset(0, 6).FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="="""""
rng.Offset(0, 6).FormatConditions(1).Interior.ColorIndex = 20
于 2013-01-25T07:15:58.983 に答える