UDF to return Date Components in an Array [VBA]

drpdrpdrp

New Member
Joined
Sep 9, 2021
Messages
20
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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
How are you entering the formula into the sheet?
 
Upvote 0
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: 13
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,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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