Summing up reference a date

LEG_END

Board Regular
Joined
Jan 8, 2017
Messages
65
I have a worksheet that has almost 500,000 lines of data. I want to sum up all the data for each day.

My dates are stored in the format dd/mm/yyyy and are in column M, my values to be summed up are in column H...
and my output should show in column N...

My code so far doesn't seem to work mind once i get to my For/Next statement any ideas?

Code:
Sub Fuel******()

Dim WS As Worksheet
Dim MaxRow As Long, I As Long
Dim Tot As Double
Dim Dte As String


Set WS = Sheets("Diesel Collaboration")
WS.Range("M3").Select
MaxRow = WS.Range("M" & WS.Rows.Count).End(xlDown).Row
Tot = 0


WS.Range("L:L").ClearContents
Sheets("Diesel Collaboration").Columns(1).Copy
Sheets("Diesel Collaboration").Columns(12).PasteSpecial xlPasteValues


WS.Range("N4:N1048576").ClearContents


Dte = WS.Cells(4, "M")


For I = 1 To MaxRow + 1
    If WS.Range("M" & I) <> Dte Then
        WS.Cells(I - 1, "H") = Tot
        Dte = WS.Cells(I, "M")
        Tot = 0
    End If
    
    Tot = Tot + Val(WS.Cells(I, "H"))


Next I


MsgBox ("Totals inserted in Col M by date successfully.")


End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try with this.
I guess the records start in row 4


Code:
Sub Fuel()


    Dim WS As Worksheet
    Dim MaxRow As Long, i As Long
    Dim Tot As Double
    Dim Dte As String
    
    Set WS = Sheets("Diesel Collaboration")
    
    MaxRow = WS.Range("M" & WS.Rows.Count).End([COLOR=#0000ff]xlUp[/COLOR]).Row
    Tot = 0
    
    WS.Range("L:L").ClearContents
    Sheets("Diesel Collaboration").Columns(1).Copy
    Sheets("Diesel Collaboration").Columns(12).PasteSpecial xlPasteValues
    
    WS.Range("N:N").ClearContents
    
    Dte = WS.Cells(4, "M")
    
    For i = 4 To MaxRow + 1
        If WS.Range("M" & i) <> Dte Then
            WS.Cells(i - 1, "[COLOR=#0000ff]N[/COLOR]") = Tot
            Dte = WS.Cells(i, "M")
            Tot = 0
        End If
        Tot = Tot + Val(WS.Cells(i, "H"))
    Next i
    
    MsgBox ("Totals inserted in Col M by date successfully.")


End Sub



I recommend you do the test with some records. This macro with For-Next for 500,000 will be very slow, I recommend you adjust the macro using objects in memory. But first do the tests with the 500,000 and tell me.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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