Rushil Sablania
Rushil Sablania

Reputation: 536

Find the number of days passed since January 1 of a particular year in vba

I want to find the number of days passed since January 1 from a date I provide.

Example :

Input - 12/01/2015
Output - 12

Input - 02/02/2015
Output - 33

I need the shortest possible way to do this. Please tell me if there is any function available in vb for this.

Upvotes: 0

Views: 1590

Answers (3)

Bond
Bond

Reputation: 16311

In VBA, just subtract the dates:

intDays = dt1 - #1/1/2015#    ' Date literals use #

Dates are stored internally in VBA as Double, with "days" as the integer portion and "time" as the decimal portion. You can always just subtract one from another to determine the difference in number of days.

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234715

DatePart("y", dt) is one way, and possibly the shortest, where dt is your date.

Alternatively, you can use =A1 - DATE(YEAR(A1), 1, 0) directly on a worksheet, where A1 holds a date.

Upvotes: 0

Ditto
Ditto

Reputation: 3344

This should do it: (input in cell C4 .. or replace C4 with your input)

=C4-DATE(YEAR(C4)-1,12,31)

(I looked at DAYS360() first, but that one isn't the same math OP needs, so not a good choice there)

and yeah, no vba needed .. I'd just stick with a normal formula if you can ;) VBA can be a bit slower sometimes, and harder to maintain ...

Upvotes: 1

Related Questions