VBA for Dates Computation is too Slow

drpdrpdrp

New Member
Joined
Sep 9, 2021
Messages
22
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
The following VBA UDF returns a 4-value-array using a Date value as input. Even though it is simple, if I ran for ~10'000 rows calculation time is not satisfactory (>30seconds).
What am I doing wrong and how should I write the code to make it faster?

VBA Code:
' YrMoDoy
Function YrMoDoyDom(InputDate)

    Dim Yr As Variant
    Dim Mo As Variant
    Dim Doy As Variant
    Dim Woy As Variant
   
    Yr = Year(InputDate)
    Mo = Month(InputDate)
    Doy = InputDate - DateSerial(Year(InputDate), 1, 0)
    Dom = Day(InputDate)
    'Woy = WorksheetFunction.IsoWeek(InputDate)

    YrMoDoyDom = Array(Yr, Mo, Doy, Dom)
End Function
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You can change the function to act on the whole column of InputDates with one function call. Invoke as =YrMoDoyDom(A1:A10000) and enter with ctrl-shift-enter not just enter. Should fill almost instantaneously.

VBA Code:
Function YrMoDoyDom(InputDate As Range)
    Dim i As Long, rows As Long, r
    r = InputDate
    rows = UBound(r)
    ReDim a(1 To rows, 1 To 4)
    For i = 1 To rows
        a(i, 1) = Year(r(i, 1))
        a(i, 2) = Month(r(i, 1))
        a(i, 3) = r(i, 1) - DateSerial(Year(r(i, 1)), 1, 0)
        a(i, 4) = Day(r(i, 1))
    Next i
    
    YrMoDoyDom = a
End Function
 
Upvote 0
Solution
You have functions and also matrix, which are going to be executing each change in the sheet.
I suggest you change the code to macro, and run it every time you want a data update.
The execution for 10,000 records is immediate.
Fit column B and the result in column C

VBA Code:
Sub MacroX()
  Dim a As Variant, b As Variant
  Dim i As Long
  a = Range("B2", Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  For i = 1 To UBound(a, 1)
    b(i, 1) = Year(a(i, 1))
    b(i, 2) = Month(a(i, 1))
    b(i, 3) = a(i, 1) - DateSerial(Year(a(i, 1)), 1, 0)
    b(i, 4) = Day(a(i, 1))
  Next
  Range("C2").Resize(UBound(b, 1), 4).Value = b
End Sub
 
Upvote 0
You can change the function to act on the whole column of InputDates with one function call. Invoke as =YrMoDoyDom(A1:A10000) and enter with ctrl-shift-enter not just enter. Should fill almost instantaneously.

VBA Code:
Function YrMoDoyDom(InputDate As Range)
    Dim i As Long, rows As Long, r
    r = InputDate
    rows = UBound(r)
    ReDim a(1 To rows, 1 To 4)
    For i = 1 To rows
        a(i, 1) = Year(r(i, 1))
        a(i, 2) = Month(r(i, 1))
        a(i, 3) = r(i, 1) - DateSerial(Year(r(i, 1)), 1, 0)
        a(i, 4) = Day(r(i, 1))
    Next i
   
    YrMoDoyDom = a
End Function
Ohhh, makes sense. Read the whole array onto memory (as "r") and then calculate on it.
Any specific reason why *r* was not declared as a specific type?
 
Upvote 0
You have functions and also matrix, which are going to be executing each change in the sheet.
I suggest you change the code to macro, and run it every time you want a data update.
The execution for 10,000 records is immediate.
Fit column B and the result in column C

VBA Code:
Sub MacroX()
  Dim a As Variant, b As Variant
  Dim i As Long
  a = Range("B2", Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  For i = 1 To UBound(a, 1)
    b(i, 1) = Year(a(i, 1))
    b(i, 2) = Month(a(i, 1))
    b(i, 3) = a(i, 1) - DateSerial(Year(a(i, 1)), 1, 0)
    b(i, 4) = Day(a(i, 1))
  Next
  Range("C2").Resize(UBound(b, 1), 4).Value = b
End Sub

You have functions and also matrix, which are going to be executing each change in the sheet.
I suggest you change the code to macro, and run it every time you want a data update.
The execution for 10,000 records is immediate.
Fit column B and the result in column C

VBA Code:
Sub MacroX()
  Dim a As Variant, b As Variant
  Dim i As Long
  a = Range("B2", Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  For i = 1 To UBound(a, 1)
    b(i, 1) = Year(a(i, 1))
    b(i, 2) = Month(a(i, 1))
    b(i, 3) = a(i, 1) - DateSerial(Year(a(i, 1)), 1, 0)
    b(i, 4) = Day(a(i, 1))
  Next
  Range("C2").Resize(UBound(b, 1), 4).Value = b
End Sub
Thank you DanteAmor. I understand the logic of loading the entire array onto memory before running calculations now.
Unfortunately this one has to be a UDF but I could get that working from your + JGordon11's reply. :)
 
Upvote 0
Ohhh, makes sense. Read the whole array onto memory (as "r") and then calculate on it.
Any specific reason why *r* was not declared as a specific type?
r needed to be of variant type to accept the array of values stored in the range. If you don't specify the data type VBA defaults to variant
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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