Incorrect Value being Applied To Cell From Code

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider this sample of data from my database:


Excel 2010
ABCDEFGHIJKLMNO
1Record ID$DateCONTRACTAmmnt$Type$Program$Customer$Facility$SubFacility$Facility B$FacType$Unit$CLASS$StartEnd
254144206417-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 1RIM Park Outdoor FacilitiesBeach Volleyball Court 1Court1B6:00 PM7:15 PM
264144206417-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 1RIM Park Outdoor FacilitiesBeach Volleyball Court 1Court1B7:15 PM8:00 PM
274144206517-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 2RIM Park Outdoor FacilitiesBeach Volleyball Court 2Court2B6:00 PM7:15 PM
284144206517-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 2RIM Park Outdoor FacilitiesBeach Volleyball Court 2Court2B7:15 PM8:00 PM
294144206617-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 3RIM Park Outdoor FacilitiesBeach Volleyball Court 3Court3B6:00 PM7:15 PM
304144206617-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 3RIM Park Outdoor FacilitiesBeach Volleyball Court 3Court3B7:15 PM8:00 PM
314144206717-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 4RIM Park Outdoor FacilitiesBeach Volleyball Court 4Court4B7:15 PM8:00 PM
324144206717-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 4RIM Park Outdoor FacilitiesBeach Volleyball Court 4Court4B7:15 PM8:00 PM
Sheet1


And this code ...
Code:
    With wshhold
        If .FilterMode Then .ShowAllData
        lr = .Range("A" & .Rows.Count).End(xlUp).Row
        With .Range("CT2:CT" & lr)
            .Formula = "=IF($CS3<>$CS2, 0, IF(ABS($N3-$O2)<=TIME(0,15,0), ""DUP"", 0))"
            .Value = .Value
        End With

        For Rw = lr To 2 Step -1 '!!!!! WATCH THIS LINE !!!!!
            If .Range("CT" & Rw) = "DUP" Then
                .Range("O" & Rw - 1).Value = .Range("O" & Rw).Value
                lmrg = lmrg + 1
            End If
        Next Rw

        With .Range("CT3:CT" & lr)
            On Error Resume Next
            .SpecialCells(xlConstants, 2).EntireRow.Delete xlShiftUp
            .Clear
        End With
            MsgBox lmrg & " records merged."
            wshvar.Range("A37").Value = lmrg
            If .FilterMode Then .ShowAllData
    End With

The idea is to find two or more records at the same facility that start within 15 minutes of the one before it ending, to become one record. An example using the data above.

146031RIM Park Outdoor FacilitiesBeach Volleyball Court 1 has two records. The first is 6:00PM-7:15PM, and the second, is 7:15PM-8:00PM

The code is intended to merge the two records into one time period for 6:00PM-8:00PM. Basically, it involves taking the later of the end times and overwriting the end time of the first record, and eliminating all but the first.

In this case, the two records would become one:
146031RIM Park Outdoor FacilitiesBeach Volleyball Court 1 6:00PM-8:00PM

When the code completes ... this is the result:


Excel 2010
ABCDEFGHIJKLMNO
254144206417-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 1RIM Park Outdoor FacilitiesBeach Volleyball Court 1Court1B7:15 PM7:15 PM
264144206517-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 2RIM Park Outdoor FacilitiesBeach Volleyball Court 2Court2B7:15 PM7:15 PM
274144206617-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 3RIM Park Outdoor FacilitiesBeach Volleyball Court 3Court3B7:15 PM7:15 PM
284144206717-Jun-131460310CRCARL Beach Volleyball 2013RIM Customer ServiceRIM Park Outdoor FacilitiesBeach Volleyball Court 4RIM Park Outdoor FacilitiesBeach Volleyball Court 4Court4B7:15 PM8:00 PM
HOLDING


The times are wrong. (Columns N and O). They should be 6:00PM - 8:00PM.

Is anyone able to figure out my error? Is more information needed?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Special K-99 ...

I don't quite understand your comment. They are visible on the data worksheet, no columns have been hidden.
 
Upvote 0
I suspect it may have something to do with this:
Code:
With .Range("CT2:CT" & lr)
            .Formula = "=IF($CS3<>$CS2, 0, IF(ABS($N3-$O2)<=TIME(0,15,0), ""DUP"", 0))"
            .Value = .Value
        End With

If we look at column CS, with our original data ... I believe things will work if CS26, CS28, CS30 and CS32 are assigned DUP. For some reason, CS25, CS27, CS29 and CS31 are being labelled the DUPs. Of the multiple rows, the first one shouldn't be DUP'd.
 
Upvote 0
I think I got it ....
Rich (BB code):
With .Range("CT2:CT" & lr)
            .Formula = "=IF($CS2<>$CS1, 0, IF(ABS($N2-$O1)<=TIME(0,15,0), ""DUP"", 0))"
            .Value = .Value
End With
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top