Ark68
Well-known Member
- Joined
- Mar 23, 2004
- Messages
- 4,564
- Office Version
- 365
- 2016
- Platform
- Windows
Consider this raw data ...
I consider two (or more) records to be "progressive when they share common contract #s and FacilityB$, and the end time of the successive program is equal to, or within 15 minutes, of the end time of the previous record. In the sample database, these progressive records are determined with the formula =IF($CR33<>$CR32,"",IF(ABS($N33-$O32)<=TIME(0,15,0),"DUP","")) in column CT.
As we refer to the data, progressive records would be row 32/33, 34/35, 36/37 and 38/39. They share a common contract number and facility (a result of a concatenation in column CR). Row 11/12, although sharing common contract and facility, they differ in that the start time of row12 is greater than 15 minutes of the end time of row 11.
The purpose of identifying those records that are progressive, is to combine them into one resulting in one record in which the earliest start time of the progressive records, and the latest end time of the progressive record is used to create one record.
In our example ... the dataset would be reduced to this ...
This process works very well, eliminating the redundant records and merging appropriately, however, problems arise when the times are substituted.
I use this code (in blue) to exchange the end times ...
Here is the contents of pivottable2:
The resulting data:
In most part it works, but the time of non-progressive records is incorrectly changed. In this case, the end time of row11 should be 12:00PM ... it should have not changed.
The question: Does anyone have any solution as how to avoid inadvertently changing the end times on for progressive records. I think the Pivottable is a bit too generic.
Jenn
Excel Workbook | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
B | F | J | N | O | CR | CS | CT | |||
1 | * | * | * | * | * | * | * | * | ||
2 | CONTRACT | Program$ | Facility B$ | Start | End | dupphase1 | dupphase2 | dupphase3 | ||
11 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 9:30 AM | 12:00 PM | 62267Hillside ParkUpper Diamond | TRUE | * | ||
12 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 1:00 PM | 3:30 PM | 62267Hillside ParkUpper Diamond | TRUE | * | ||
32 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | 6:30 PM | 7:15 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | TRUE | * | ||
33 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | 7:15 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | TRUE | DUP | ||
34 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | 6:30 PM | 7:15 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | TRUE | * | ||
35 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | 7:15 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | TRUE | DUP | ||
36 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | 6:30 PM | 7:15 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | TRUE | * | ||
37 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | 7:15 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | TRUE | DUP | ||
38 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | 6:30 PM | 7:15 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | TRUE | * | ||
39 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | 7:15 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | TRUE | DUP | ||
Data |
I consider two (or more) records to be "progressive when they share common contract #s and FacilityB$, and the end time of the successive program is equal to, or within 15 minutes, of the end time of the previous record. In the sample database, these progressive records are determined with the formula =IF($CR33<>$CR32,"",IF(ABS($N33-$O32)<=TIME(0,15,0),"DUP","")) in column CT.
As we refer to the data, progressive records would be row 32/33, 34/35, 36/37 and 38/39. They share a common contract number and facility (a result of a concatenation in column CR). Row 11/12, although sharing common contract and facility, they differ in that the start time of row12 is greater than 15 minutes of the end time of row 11.
The purpose of identifying those records that are progressive, is to combine them into one resulting in one record in which the earliest start time of the progressive records, and the latest end time of the progressive record is used to create one record.
In our example ... the dataset would be reduced to this ...
Excel Workbook | |||||||||
---|---|---|---|---|---|---|---|---|---|
B | F | J | N | O | CR | CS | |||
1 | * | * | * | * | * | * | * | ||
2 | CONTRACT | Program$ | Facility B$ | Start | End | dupphase1 | dupphase2 | ||
11 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 9:30 AM | 12:00 PM | 62267Hillside ParkUpper Diamond | TRUE | ||
12 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 1:00 PM | 3:30 PM | 62267Hillside ParkUpper Diamond | TRUE | ||
32 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | 6:30 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | TRUE | ||
33 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | 6:30 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | TRUE | ||
34 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | 6:30 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | TRUE | ||
35 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | 6:30 PM | 8:00 PM | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | TRUE | ||
Data |
This process works very well, eliminating the redundant records and merging appropriately, however, problems arise when the times are substituted.
I use this code (in blue) to exchange the end times ...
Rich (BB code):
Sub merged_duplicates()
'
' **** ELIMINATE And MERGE DUPLICATE RECORDS ****
'
Application.EnableEvents = False
Dim dupholder As Worksheet
Dim rngToCopy As Range
Dim rgnToDelete As Range
Dim llastrow As Long
Dim lmergereccopy As Long
Dim lmergedelete As Long
llastrow = 0
lmergereccopy = 0
Label1 = "IDENTIFYING PROGRESSIVE RECORDS"
With core_data
.Activate
.Unprotect
If .FilterMode Then .ShowAllData 'if main data is filtered, unfilter it
'.Range("CT3:CT" & .Range("B" & Rows.Count).End(xlUp).Row).Formula = "=IF($CR3=$CR2,""DUP"","""")"
.Range("CT3:CT" & .Range("B" & Rows.Count).End(xlUp).Row).Formula = "=IF($CR3<>$CR2,"""",if(ABS($N3-$O2)<=time(0,15,0),""DUP"",""""))" 'dupphase3
llastrow = .Range("a65536").End(xlUp).Row 'determine the last row number
'With .Range("CR5:CR" & llastrow) 'identify the the range (A5 to DI of the last row)
lmergereccopy = WorksheetFunction.CountIf(.Range("CT2:CT" & llastrow), "DUP")
'End With
' if there are no duplicates than no need to merge any. Finish.
If lmergereccopy = 0 Then
'MsgBox "There are no records to be merged.", , "NO DUPLICATES"
TextBox5.Visible = True
TextBox5.Value = 0
TextBox5.Locked = True
If .FilterMode Then .ShowAllData 'return database to display all data
Exit Sub
End If
End With
MsgBox (lmergereccopy & " records identified to be merged.")
Label1 = "MERGING RECORDS."
With wshref
.Unprotect
'.Activate
'pivottable determines the number of duplicates per contract number and the mimimum start time from same. Where there are multiple entries, the earliest start time for that contract will be displayed
.PivotTables("PivotTable5").PivotCache.Refresh
.Protect
End With
With core_data
.Activate
.Range("D2").Select
Dim rngToDelete2 As Range
llastrow = 0
lmergedelete = 0
llastrow = .Range("a65536").End(xlUp).Row 'calculate the last row visible
'filter the visible rows to include only those rows in which dupphase3 = "DUP" (ie. duplicated rows)
With .Range("A2:DI" & llastrow)
.AdvancedFilter _
Action:=xlFilterInPlace, _
CriteriaRange:=Worksheets("Reference").Range("A29:A30"), _
Unique:=False
On Error Resume Next
Set rngToDelete2 = .Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible) 'this range is the group of visible rows representing those found from the filter
On Error GoTo 0
'lmergedelete = .rngToDelete.Count 'how many rows in the visible range of data
lmergedelete = llastrow - WorksheetFunction.Count(core_data.Columns("A"))
MsgBox lmergedelete & (" Duplicates being merged.")
TextBox5.Visible = True
TextBox5.Value = lmergedelete
TextBox5.Locked = True
TextBox6 = TextBox6 - lmergedelete
If Not rngToDelete2 Is Nothing Then rngToDelete2.EntireRow.Delete 'delete the visible rows (ie the duplicated)
Label1 = "Extraneous records from merging removed."
End With
With core_data
If .FilterMode Then .ShowAllData ' show all data. All duplicates are removed, leaving only one of the duplicated contracts in the database
.Range("O3:O" & .Range("A" & Rows.Count).End(xlUp).Row).Formula = "=VLOOKUP($CR3,DatabaseRG3,3,FALSE)" 'using the min times from the pivottable, replace existing start times with min.
.Protect
End With
End With
End Sub
Here is the contents of pivottable2:
Excel Workbook | |||||
---|---|---|---|---|---|
AD | AE | AF | |||
1 | * | Data | * | ||
2 | dupphase1 | Count of End | Max of End2 | ||
3 | 60403RIM Park Outdoor FacilitiesAF-1 (Artificial Field) | 1 | 9:00 PM | ||
4 | 60403RIM Park Outdoor FacilitiesAF-2 (Artificial Field) | 2 | 11:00 PM | ||
5 | 60933Waterloo ParkWP - Ball Diamond #1 | 1 | 11:00 PM | ||
6 | 60933Waterloo ParkWP - Ball Diamond #2 | 1 | 11:00 PM | ||
7 | 60791Auburn ParkBall Diamond | 1 | 8:15 PM | ||
8 | 60818Cedarbrae Public SchoolBall Diamond #1 | 1 | 8:30 PM | ||
9 | 60818Cedarbrae Public SchoolBall Diamond #2 | 1 | 8:30 PM | ||
10 | 60962Conservation MeadowsField | 1 | 8:30 PM | ||
11 | 60655Creekside Church FieldsField 1 | 1 | 8:15 PM | ||
12 | 60655Creekside Church FieldsField 2 | 1 | 8:15 PM | ||
13 | 61042Hillside ParkLower Diamond | 1 | 8:15 PM | ||
14 | 60800Lexington Road ParkBall Diamond | 1 | 8:15 PM | ||
15 | 60653Lexington Road ParkSouth Field | 1 | 8:15 PM | ||
16 | 60651Northfield ParkField | 1 | 8:15 PM | ||
17 | 60654Northfield Pond ParkField | 1 | 8:15 PM | ||
18 | 60655Old Oak ParkField - 1 | 1 | 8:15 PM | ||
19 | 60655Old Oak ParkField - 2 | 1 | 8:15 PM | ||
20 | 60799Regency ParkBall Diamond | 1 | 8:15 PM | ||
21 | 60746RIM Park Outdoor FacilitiesBall Diamond #1 | 1 | 9:30 PM | ||
22 | 60746RIM Park Outdoor FacilitiesBall Diamond #2 | 1 | 9:30 PM | ||
23 | 60794RIM Park Outdoor FacilitiesBall Diamond #3 | 1 | 10:15 PM | ||
24 | 60746RIM Park Outdoor FacilitiesBall Diamond #4 | 1 | 8:00 PM | ||
25 | 60746RIM Park Outdoor FacilitiesBall Diamond #5 | 1 | 8:00 PM | ||
26 | 60746RIM Park Outdoor FacilitiesBall Diamond #6 | 1 | 8:00 PM | ||
27 | 60652RIM Park Outdoor FacilitiesField #1 | 1 | 8:15 PM | ||
28 | 60653RIM Park Outdoor FacilitiesField #10 | 1 | 8:15 PM | ||
29 | 60652RIM Park Outdoor FacilitiesField #2 | 1 | 8:15 PM | ||
30 | 60652RIM Park Outdoor FacilitiesField #3 | 1 | 8:15 PM | ||
31 | 60652RIM Park Outdoor FacilitiesField #4 | 1 | 8:15 PM | ||
32 | 60651RIM Park Outdoor FacilitiesField #7 | 1 | 8:15 PM | ||
33 | 60651RIM Park Outdoor FacilitiesField #8 | 1 | 8:15 PM | ||
34 | 60653RIM Park Outdoor FacilitiesField #9 | 1 | 8:15 PM | ||
35 | 60769Waterloo ParkCricket Pitch | 1 | 8:30 PM | ||
36 | 60962Waterloo ParkEast Field | 1 | 8:30 PM | ||
37 | 60725Waterloo ParkWest Field | 1 | 8:15 PM | ||
38 | 60796Waterloo ParkWP - Ball Diamond #3 | 1 | 8:15 PM | ||
39 | 60801Westwind ParkBall Diamond | 1 | 8:15 PM | ||
40 | 60929Waterloo ParkWP - Ball Diamond #1 | 1 | 2:00 PM | ||
41 | 60929Waterloo ParkWP - Ball Diamond #2 | 1 | 2:00 PM | ||
42 | 60929Waterloo ParkWP - Ball Diamond #3 | 1 | 2:00 PM | ||
43 | 60929Waterloo ParkWP - Ball Diamond #4 | 1 | 2:00 PM | ||
44 | 61482RIM Park Outdoor FacilitiesAF-1 (Artificial Field) | 1 | 11:00 PM | ||
45 | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | 2 | 8:00 PM | ||
46 | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | 2 | 8:00 PM | ||
47 | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | 2 | 8:00 PM | ||
48 | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | 2 | 8:00 PM | ||
49 | 61663Waterloo ParkWP - Ball Diamond #4 | 1 | 8:00 PM | ||
50 | 60533Waterloo ParkWest Field | 1 | 11:30 AM | ||
51 | 60746Hillside ParkLower Diamond | 1 | 10:00 PM | ||
52 | 62267Hillside ParkUpper Diamond | 2 | 3:30 PM | ||
53 | 61047Hillside ParkUpper Diamond | 1 | 8:30 PM | ||
54 | 60746Hillside ParkUpper Diamond | 1 | 10:00 PM | ||
55 | 61570RIM Park Outdoor FacilitiesField #7 | 1 | 11:00 AM | ||
Reference |
The resulting data:
Excel Workbook | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
B | F | J | N | O | P | CR | CS | CT | |||
1 | * | * | * | * | * | * | * | * | * | ||
2 | CONTRACT | Program$ | Facility B$ | Start | End | Charges | dupphase1 | dupphase2 | dupphase3 | ||
11 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 9:30 AM | 3:30 PM | 3.78 | 62267Hillside ParkUpper Diamond | TRUE | * | ||
12 | 62267 | College Pro Baseball Acedemy Camp | Hillside ParkUpper Diamond | 1:00 PM | 3:30 PM | 3.78 | 62267Hillside ParkUpper Diamond | TRUE | * | ||
31 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | 6:30 PM | 8:00 PM | * | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 1 | FALSE | * | ||
32 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | 6:30 PM | 8:00 PM | * | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 2 | FALSE | #REF! | ||
33 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | 6:30 PM | 8:00 PM | * | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 3 | FALSE | #REF! | ||
34 | 131229 | CARL Beach Volleyball | RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | 6:30 PM | 8:00 PM | * | 131229RIM Park Outdoor FacilitiesBeach Volleyball Court 4 | FALSE | #REF! | ||
Data |
In most part it works, but the time of non-progressive records is incorrectly changed. In this case, the end time of row11 should be 12:00PM ... it should have not changed.
The question: Does anyone have any solution as how to avoid inadvertently changing the end times on for progressive records. I think the Pivottable is a bit too generic.
Jenn