UDF to return Date Components in an Array [VBA]

drpdrpdrp

New Member
Joined
Sep 9, 2021
Messages
22
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I want to return the date components (Year, Month, DayOfYear, DayOfMonth) using a UDF.
Can someone explain why the code below won't work ?

VBA Code:
Function DateYrMoDoyDom(InputDate As Range)
    'makes it easier to extract Year, Month, DayOfYear, DayOfMonth from date
    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
    
    DateYrMoDoyDom = a
End Function
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
And how many cells are there in "InputDate"?
 
Upvote 0
When you say "won't work", what does that mean? Exactly what happens when you try to use your UDF?
 
Upvote 0
Along with Rick's question, are the 4 cells in one row, one column, or something else?
 
Upvote 0
#VALUE! error, Rick.
I've attached an image, which should explain better. Let me know if I should send the workbook
 

Attachments

  • error_UDF_DateComponents.PNG
    error_UDF_DateComponents.PNG
    49.5 KB · Views: 16
Upvote 0
As The input is a single cell use
VBA Code:
Function DateYrMoDoyDom(InputDate As Range)
    'makes it easier to extract Year, Month, DayOfYear, DayOfMonth from date
    Dim i As Long, rows As Long, r
    r = InputDate
    ReDim A(1 To 1, 1 To 4)
        A(1, 1) = Year(r)
        A(1, 2) = Month(r)
        A(1, 3) = r - DateSerial(Year(r), 1, 0)
        A(1, 4) = Day(r)
    
    
    DateYrMoDoyDom = A
End Function
 
Upvote 0
Solution
NOTE: I deleted my previous message as I developed a more compact solution

Like Fluff said, since the input is a single cell, here is another UDF that you can try...
VBA Code:
Function DateYrMoDoyDom(DateIn As Range) As Variant
  DateYrMoDoyDom = Split(Format(DateIn, "yyyy mm y dd"))
End Function
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,315
Members
452,634
Latest member
cpostell

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