Macro of SUMIFS Date & Time cell reference for large data

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
121
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have a large data ranging between 200k-400k row every weekday to analyze.
I need to get result for Column P, Q, R, S, T, U , V, W, X, Y from Row 2 to Row 7201 (sometimes until Row 1441 , 481 ,241 ,121 depend on what kind of analyze is).
Current formula I use is " =SUMIFS($B$2:$B$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2) " for Columm P.
Every Column P, Q, R, S, T, U , V, W, X, Y use same Date&Time cell reference Column N and 0
Date and Time data is on Column A while data for Column P, Q, R, S, T, U , V, W, X, Y are Column B, C, D, E, F, G, H, I , J ,K respectively.

Current time to complete calculation for one Column P is 3:30 minute, total of 9 column is average of 30 minutes.

This is some sample from data :
sumifs ab.xlsb
ABCDEFGHIJKLMNOPQRSTUVWXY
1local timea0+1a0-1b0-1b0+1a+a-a0b-b+b0.M1A0+1A0-1B0-1B0+1a+a-a0b-b+b0
218.10.21 5:0018.10 5:0118.10 5:00121310733611
318.10.21 5:001118.10 5:0218.10 5:01
418.10.21 5:001118.10 5:0318.10 5:02
518.10.21 5:001118.10 5:0418.10 5:03
618.10.21 5:001118.10 5:0518.10 5:04
718.10.21 5:0011118.10 5:0618.10 5:05
818.10.21 5:0011118.10 5:0718.10 5:06
918.10.21 5:0011118.10 5:0818.10 5:07
1018.10.21 5:001118.10 5:0918.10 5:08
1118.10.21 5:001118.10 5:1018.10 5:09
1218.10.21 5:001118.10 5:1118.10 5:10
1318.10.21 5:001118.10 5:1218.10 5:11
1418.10.21 5:001118.10 5:1318.10 5:12
1518.10.21 5:001118.10 5:1418.10 5:13
1618.10.21 5:0011118.10 5:1518.10 5:14
1718.10.21 5:001118.10 5:1618.10 5:15
1818.10.21 5:001118.10 5:1718.10 5:16
1918.10.21 5:0011118.10 5:1818.10 5:17
2018.10.21 5:0011118.10 5:1918.10 5:18
2118.10.21 5:001118.10 5:2018.10 5:19
a0+1b0-1
Cell Formulas
RangeFormula
P2P2=SUMIFS($B$2:$B$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
Q2Q2=SUMIFS($C$2:$C$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
R2R2=SUMIFS($D$2:$D$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
S2S2=SUMIFS($E$2:$E$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
T2T2=SUMIFS($F$2:$F$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
U2U2=SUMIFS($G$2:$G$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
V2V2=SUMIFS($H$2:$H$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
W2W2=SUMIFS($I$2:$I$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
X2X2=SUMIFS($J$2:$J$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
Y2Y2=SUMIFS($K$2:$K$400000,$A$2:$A$400000,">="&O2,$A$2:$A$400000,"<"&N2)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this custom code below. It runs very fast.
It takes 3 parameters
rangeToSum = the range to sum, B2:K400000
rangeTime = the list of times, A2:A400000
rangeLow=the list of times you want to match, O2:O50000 (assuming you have 50000 rows in O, change this)
So for your spreadsheet, use this formula in P2 (leave Q2, etc blank)
Excel Formula:
=xsumifs(B2:K400000,A2:A400000,O2:O50000)
Assuming you have a reasonably up to date Excel version, It should fill the range P2:Y2 and the rows below, ie one formula does it all

VBA Code:
Function xSumiFS(rangeToSum, rangeTime, rangeLow)
  Dim arrSum, i As Long, j As Long, k As Long, n As Long, arrLow, arrTime, dict, sumCols
  arrSum = rangeToSum
  arrLow = rangeLow
  arrTime = rangeTime
  Set dict = CreateObject("scripting.dictionary")
  sumCols = UBound(arrSum, 2)

  For i = 1 To UBound(arrLow)
    dict.Add arrLow(i, 1), i
  Next i

  'now we run through the data
  ReDim A(1 To UBound(arrLow), 1 To sumCols) As Single 'output list
  For i = 1 To UBound(arrSum)
    j = dict(arrTime(i, 1))
    If j > 0 Then
      For k = 1 To sumCols
        A(j, k) = A(j, k) + arrSum(i, k)
      Next k
    End If
  Next i
  xSumiFS = A
End Function
 
Upvote 0
I think Dermot's answer is the way to go. I did the following purely as a coding exercise. Tested with 400K of data in col A x 7201 rows in col P. Takes about 45 seconds to do all 10 columns. Assumes columns AA:AK are free to use as helper columns.

VBA Code:
Sub ibmy()
    Dim rng As Range, r As Range, ws As Worksheet
    Dim i As Long, j As Long, n As Long, lr1 As Long, lr2 As Long, x As Long
    Dim txt As String
    Dim ar, t
    t = Timer
    
    Application.Calculation = xlManual
    Application.EnableEvents = False
    Application.DisplayStatusBar = False
    
    Set ws = Sheet1 '<~~ change to suit
    Set rng = ws.Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
    ar = ws.Range("A1").CurrentRegion

    With CreateObject("scripting.dictionary")
        For Each r In rng
            txt = r.Value
        If Not .Exists(txt) Then
            n = n + 1
            .Add txt, n
            For j = 1 To UBound(ar, 2)
                ar(n, j) = r.Offset(, j - 1)
            Next j
        Else
        For i = 2 To UBound(ar, 2)
            ar(.Item(txt), i) = ar(.Item(txt), i) + r.Offset(, i - 1)
        Next i
        End If
        Next

    ws.Range("AA1").Resize(n - 1, UBound(ar, 2)) = ar
    End With
    
    lr1 = ws.Cells(Rows.Count, 15).End(xlUp).Row
    lr2 = ws.Cells(Rows.Count, 27).End(xlUp).Row
    
    n = 2
    x = 2
    For n = 2 To 11
    With ws.Range(ws.Cells(2, n + 14), ws.Cells(lr1, n + 14))
        .FormulaR1C1 = "=if(vlookup(RC15,R2C27:R" & lr2 & "C27,1)=RC15,vlookup(RC15,R2C27:R" & lr2 & "C37," & x & "),"""")"
        .Value2 = .Value2
    End With
        x = x + 1
    Next n
    
    ws.Range("AA1:AK1").EntireColumn.ClearContents
    Application.Calculation = xlAutomatic
    Application.EnableEvents = True
    MsgBox Timer - t
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,176
Members
449,491
Latest member
maxim_sivakon

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