VBA to find the day number of the year from today's date?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can someone help me?

I would like a VBA code that can find the day number of the year based from the current date.

Example: today is the 05/10/2019 so I would like the code to return 278. So tomorrow would return 279 and so on.

I have the date in cell T3 if that helps any?

Any help would be appreciated.

Thanks

Dan
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Perfect thank you.

Can I ask, can your code be modified so it can add the following please?

I would like a 9 before the 278 and then 1092 afterwards.

Is that possible?

Thanks again

Dan
 
Upvote 0
Hi Rick,

I did, but when AlphaFrog kindly supplied me with a formula I just started recording a macro. I entered the formula in my cell and then I pressed F2 and then enter and that gave me the formula in VBA.

Thanks

Dan
 
Upvote 0
The day number is formatted as three digits; 001, 002, ...365

You could change that to suit.

Declare dn as String if you want a text result, or Long if you want a numeric value.

Code:
dn = "9" & Format(Date - DateSerial(Year(Date), 1, 0), "000") & "1092"
 
Last edited:
Upvote 0
Code:
dn = "9" & Format(Date - DateSerial(Year(Date), 1, 0), "000") & "1092"
You can eliminate the concatenations by moving those numbers into the format pattern taking care to put a backslash in front of any 0's that are to remain constant within the pattern...
Code:
dn = Format(Date - DateSerial(Year(Date), 1, 0), "90001\092")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,575
Messages
6,120,342
Members
448,956
Latest member
Adamsxl

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