Need Summary after every 5 Mins from Data

Nikhil J Shriyan

New Member
Joined
May 27, 2012
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I have an Order sheet that gets orders every minute. I need a Summary that orders the Total and the Current Time in Summary Sheet so that I can know the Difference in Qty and Amt every 5 Mins from the table.

Can I get a Macro so that I can get this summary automated?

Raw Files.xlsx
6
Summary
Excel Formula:
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello. FOR ME you have not provided enough information for assistance. Might you give more detail and show some data from your worksheets so we have a better idea what you are trying to accomplish.
 
Upvote 0
Book1
ABCDEFGHI
1Current Time
2TOTAL2,951139,89910:37 AM
3OrderOrder IDSKU#DateProductQTYPriceAMOUNTOrder Time
41086-0110000119/09/22ABC3216310:36 AM
51086-0210001219/09/22DEF182646810:37 AM
61086-0310002319/09/22GHI1272710:38 AM
71086-0410003419/09/22ABC162235210:39 AM
81086-0510004519/09/22DEF4218410:40 AM
91086-0610005619/09/22GHI2265210:41 AM
101086-0710006719/09/22ABC42710810:42 AM
111086-0810007819/09/22DEF52211010:43 AM
121086-0910008919/09/22GHI52713510:44 AM
131086-10100091019/09/22ABC52814010:45 AM
141086-11100101119/09/22DEF52311510:46 AM
151086-12100111219/09/22GHI52211010:47 AM
161086-13100121319/09/22ABC52713510:48 AM
171086-14100131419/09/22DEF52814010:49 AM
181086-15100141519/09/22GHI52311510:50 AM
191086-16100151619/09/22ABC52814010:51 AM
201086-17100161719/09/22DEF52914510:52 AM
211086-18100171819/09/22GHI52412010:53 AM
221086-19100181919/09/22ABC
ORDERS



Book1
ABCD
1
2
3TimeQtyAmtDifference
410:37:00 AM2,951139,8990
510:42:00 AM2,989140,399500
610:47:00 AM3,041142,8992,500
SUMMARY
Cell Formulas
RangeFormula
D5:D6D5=C5-C4
 
Upvote 0
This function will sum every five values. I HOPE that there is one minute per row? (That is what your data shows.) If not then this will not work.

The first value that you put into the function is the first amount cell in the range, with absolute addressing (with dollar signs). The second value for the formula is for the value cell in the "current" row. For example, if amount data is in range B5 to B22 then the formula in D9 would be =SumEveryNValues($B$5, B9).

VBA Code:
Function SumEveryNValues(prAnchor As Range, prCurrent As Range, Optional piInterval As Long = 5) As Variant
    
    SumEveryNValues = ""
    
    If (prCurrent.Row - prAnchor.Row) Mod piInterval = piInterval - 1 _
     Then
        SumEveryNValues = _
            Application.WorksheetFunction.Sum(prCurrent.Offset(-(piInterval - 1)).Resize(piInterval))
    End If

End Function


Book1
ABCD
2Start Time10:36 AM
3
4AmountTime
516310:36 AM 
6246810:37 AM 
732710:38 AM 
8435210:39 AM 
958410:40 AM994
1065210:41 AM 
11710810:42 AM 
12811010:43 AM 
13913510:44 AM 
141014010:45 AM545
151111510:46 AM 
161211010:47 AM 
171313510:48 AM 
181414010:49 AM 
191511510:50 AM615
201614010:51 AM 
211714510:52 AM 
221812010:53 AM 
23
Sheet1
Cell Formulas
RangeFormula
D5:D22D5=SumEveryNValues($B$5, B5)
C6:C22C6=C5+1/24/60
 
Upvote 0
This function will sum every five values. I HOPE that there is one minute per row? (That is what your data shows.) If not then this will not work.

The first value that you put into the function is the first amount cell in the range, with absolute addressing (with dollar signs). The second value for the formula is for the value cell in the "current" row. For example, if amount data is in range B5 to B22 then the formula in D9 would be =SumEveryNValues($B$5, B9).

VBA Code:
Function SumEveryNValues(prAnchor As Range, prCurrent As Range, Optional piInterval As Long = 5) As Variant
   
    SumEveryNValues = ""
   
    If (prCurrent.Row - prAnchor.Row) Mod piInterval = piInterval - 1 _
     Then
        SumEveryNValues = _
            Application.WorksheetFunction.Sum(prCurrent.Offset(-(piInterval - 1)).Resize(piInterval))
    End If

End Function


Book1
ABCD
2Start Time10:36 AM
3
4AmountTime
516310:36 AM 
6246810:37 AM 
732710:38 AM 
8435210:39 AM 
958410:40 AM994
1065210:41 AM 
11710810:42 AM 
12811010:43 AM 
13913510:44 AM 
141014010:45 AM545
151111510:46 AM 
161211010:47 AM 
171313510:48 AM 
181414010:49 AM 
191511510:50 AM615
201614010:51 AM 
211714510:52 AM 
221812010:53 AM 
23
Sheet1
Cell Formulas
RangeFormula
D5:D22D5=SumEveryNValues($B$5, B5)
C6:C22C6=C5+1/24/60
Hello Sir

Thanks this is very Dynamic


Can you Copy and Paste the Data From E2:I1 in a Separate Sheet after a time interval of 5 Mins One below the other as per the Start Time Mentioned and End time Mentioned this would help analysis
 
Upvote 0
The code below does what you asked for. The macro is pretty long and ugly. I was surprised that it took that many lines of code for me to get it working. Better programmers can definitely make it better but at least it works.

It looks in the same exact cells as your example. So data must be in the exact same location as in your example or the macro will not work. And, there cannot be any data in column J and after in the orders data worksheet. Data must be in the worksheet named "Sheet1" and results will be placed into the worksheet named Sheet2" which must exist.

I tried to write code so you could modify it if data locations change. But if you do move data then all bets are off.

Also, this code uses the function that I provided above named SumEveryNValues.

I hope that it works for you. Good luck.

VBA Code:
Sub ProcessEveryNMinutes()

    Dim wsSourceSheet As Worksheet
    Dim wsTargetSheet As Worksheet
    
'   Cell containing start time specified for data.
    Dim rStartTime As Range

'   Value of the start time for data.
    Dim dStartTime As Date

'   Cell containing first time being processed.
    Dim rFirstTime As Range

'   Value of first time being processed
    Dim dFirstTime As Date
    
'   Range where all times being processed are located.
    Dim rTimesToProcess As Range
    
'   For each iteration of results to accumulate totals for Starty Quantity and Amount.
    Dim iStartAmount As Long
    Dim iStartQty As Long

'   Cell range used for looping through ranges
    Dim rCell As Range
    
'   Use to keep track of the first and last data rows.
    Dim iFirstDataRow As Long
    Dim iLastDataRow As Long
    
'   Use to keep track of the count of row times.
    Dim iTimeRow As Long
    
'   Range where results are located. Rows in the results range.
    Dim rResults As Range
    Dim iResultRows As Long
    
'   Cell in target worksheet where results go
    Dim rTarget As Range
    
'   Number of minutes between results.
    Dim iInterval As Long
    
'    Cell address in results worksheet where results are pasted
    Dim sPasteCellAddress As String
    
'   Used when adding difference formulas to specify the previous and current
'   amounts. Need cell addresses when adding the formulas.
    Dim sPrevAmountAddress As String
    Dim sCurrentAmountAddress As String
    
'   Count of results rows in source worksheet.
    Dim iResultsRows As Long
    
'   Used to loop through all results rows.
    Dim iResultRow As Long

'   --------------------------------
'           Initializations
'   --------------------------------
    
    iInterval = 5

'   Worksheet where data is located.
    Set wsSourceSheet = ThisWorkbook.Worksheets("Sheet1")
    
'   Worksheet where resuls will be located.
    Set wsTargetSheet = ThisWorkbook.Worksheets("Sheet2")
    
    With wsSourceSheet
        
        Set rStartTime = .Range("I2")
        Set rFirstTime = .Range("I4")
        
'       Get beginning Quantity and Amount values.
        iStartQty = .Range("F2").Value
        iStartAmount = .Range("H2").Value
        
'       Get last data row.
        iLastDataRow = rStartTime.Offset(10000).End(xlUp).Row
        iLastDataRow = iLastDataRow - rFirstTime.Row + 1

'       Results header values. Four headers. Ranges to the right of cell rFirstTime.
        With rFirstTime
            .Offset(, 1).Value = "Time"
            .Offset(, 2).Value = "Quantity"
            .Offset(, 3).Value = "Amount"
            .Offset(, 4).Value = "Difference"
        End With

    End With 'wsSourceSheet
    
'   Clear all cells in the target workbook (the worksheet where results are located).
    wsTargetSheet.Cells.Value = ""
    
'   Location in results worksheet where results are located.
    Set rTarget = wsTargetSheet.Range("A3")
                
    dStartTime = rStartTime.Value

'   Range where all times are locted.
    Set rTimesToProcess = rFirstTime.Resize(iLastDataRow)
    
'   ----------------------------------
'         Get row for start time
'   ----------------------------------

    For Each rCell In rTimesToProcess

        iTimeRow = iTimeRow + 1

        If rCell.Value = dStartTime Then Exit For

    Next rCell
    
'   ---------------------------------------------
'         Set 1st Time Cell and Times ranges
'   ---------------------------------------------
    
    Set rFirstTime = rFirstTime.Offset(iTimeRow)
    
    Set rTimesToProcess = rFirstTime.Resize(iLastDataRow - iTimeRow)
    
'   -----------------------------------------------------------
'         Set first row in results area in source worksheet
'   ------------------------------------------------------------
        
'   Put start qty and start amount into first results row in the
'   source worksheet. That row is one row above the row for the
'   first time to process hence -1 row offset.
    
    With rTimesToProcess.Cells(1)
        With .Offset(-1, 1)
            .Value = dStartTime
            .NumberFormat = "h:m am/pm"
        End With
        
        With .Offset(-1, 2)
            .Value = iStartQty
            .NumberFormat = "#,##0"
        End With
        
        With .Offset(-1, 3)
            .Value = iStartAmount
            .NumberFormat = "#,##0"
        End With
        
        .Offset(-1, 4).Value = 0
    End With
        
'   ---------------------------------------------------------
'         Set results formulas area in source worksheet
'   ---------------------------------------------------------

'   Put formula to calculate n values for Quantity and Amount into
'   results cells in the source worksheet. Use times to process
'   columnar data as "anchor" location for results formulas.
    
    For Each rCell In rTimesToProcess
    
'       Clear time values.
        rCell.Offset(, 1).Value = ""
        
'       Quantity column is 2 columns over from range rTimesToProcess.
        With rCell.Offset(, 2)
            .Formula = _
                "=SumEveryNValues(" & rFirstTime.Offset(, -2).Address & ", " & rCell.Offset(, -2).Address & ", " & iInterval & ")"
            
            .NumberFormat = "#,##0"
            
'           Get that value then add it to previous sum of Quantities for next iteration.
            Application.Calculate
            
            If .Value <> "" Then
                .Value = iStartQty + .Value
                iStartQty = .Value 'for next iteration
            End If
                    
        End With
        
'       Amount column is 3 columns over from range rTimesToProcess.
        With rCell.Offset(, 3)
            .Formula = _
                "=SumEveryNValues(" & rFirstTime.Offset(, -1).Address & ", " & rCell.Offset(, -1).Address & ", " & iInterval & ")"

            .NumberFormat = "#,##0"

'           Get that value then add it to previous sum of Amounts for next iteration.
            Application.Calculate
            
            If .Value <> "" Then
                .Value = iStartAmount + .Value
                iStartAmount = .Value ' for next iteration
            End If

        End With
        
        Application.Calculate
        
'       Format time
        With rCell.Offset(, 3)
            If .Value <> "" Then rCell.Offset(, 1).Value = rCell.Value
            rCell.Offset(, 1).NumberFormat = "h:m am/pm"
        End With
    
    Next rCell

'   ----------------------------------------------------
'         Convert results formulas to plain values
'   ----------------------------------------------------
'
    With rTimesToProcess.Cells(1, 2).Resize(iLastDataRow - iTimeRow, 4)
        .Value = .Value
    End With
        
'   Point range object to range where results were placed.
    Set rResults = rTimesToProcess.Cells(1, 2).Resize(iLastDataRow - iTimeRow, 4)

'Debug.Print "rResults = " & rResults.Address

    iResultsRows = rResults.Rows.Count

'   ----------------------------------------------------------------
'         Delete empty rows in results data in soure worksheet
'   ----------------------------------------------------------------

'   Delete empty cells in results range. Loop backwards
    For iResultRow = iResultsRows - 1 To 0 Step -1

        If rResults.Cells(1).Offset(iResultRow) = "" _
         Then
            rResults.Cells(1).Offset(iResultRow).Resize(1, 4).Delete Shift:=xlUp
        End If
        
    Next iResultRow
    
'   --------------------------------------------
'         Copy results to target worksheet
'   --------------------------------------------
    
'   Get first row in results, two below start time cell
    iFirstDataRow = rStartTime.Row
        
'   Get last row in results
    iLastDataRow = rResults.Cells(1).Offset(10000).End(xlUp).Row
    
    iResultRows = iLastDataRow - iFirstDataRow - 1
    
'   Point rResults range object at the range where results are in source worksheet.
    Set rResults = rResults.Cells(1).Offset(-2).Resize(iResultRows, 4)
    
'   Copy the results
    rResults.Copy
    
'   Activate the target sheet and cell before paste
    wsTargetSheet.Activate
    rTarget.Activate
    
'   Clear existing values if any.
    rTarget.CurrentRegion.Value = ""
    
'   Put (paste) results into the specified range in target worksheet.
    ActiveSheet.Paste

    Application.CutCopyMode = False
    
'   --------------------------------------------------------
'         Clear results values in the source worksheet
'   --------------------------------------------------------
        
'   Clear the results (values) in results in source range worksheet
    rResults.Value = ""
    
'   ------------------------------------------------------------
'         Add "subtraction" formulas for difference column
'   ------------------------------------------------------------

'   Set results range to the place where results were pasted.
'   rTarget points to the upperleft cell.

    Set rResults = rTarget.CurrentRegion
    
'   Loop difference cells in results data in results worksheet and put
'   subtraction formulas into them. Format them too.
    With rResults.Cells(3, 4)
            
        For Each rCell In .Resize(iResultRows - 2)
        
            With rCell
        
'               Get cell addresses for previous row amount and current row amount.
                sPrevAmountAddress = .Offset(-1, -1).Address
                sCurrentAmountAddress = .Offset(, -1).Address

'               Formula to subtract previous row amount from current row amount to get differnce.
                .Formula = "=" & sCurrentAmountAddress & "-" & sPrevAmountAddress
                
                .NumberFormat = "#,##0"
        
            End With
            
        Next rCell

    End With
    
'   Activate cell above upperleft results data cell
    rResults.Cells(1).Offset(-1).Activate

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

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