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

danbates

Board Regular
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
 

danbates

Board Regular
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
 

danbates

Board Regular
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
 

AlphaFrog

MrExcel MVP
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:

Rick Rothstein

MrExcel MVP
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:

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Syntax errors
    Good Morning, Trying to compile a workbook, I keep getting a few errors. Here are the first two: [code=rich]Syntax Error: Function...
Top