VBA - Dates

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
256
Office Version
  1. 365
Platform
  1. Windows
I have an excel document where in column H is a list of dates, I am not concerned with the year though so want to change the date in each cell to the current year.
So for example I have a date of 20/05/2012, I want that to change to 20/05/2024. I am in the UK hence why using UK date format.

I can manually use Text to Columns to do this but isn't the best way when using VBA.

What is the best VBA code to do this please?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Do you need VBA?

Is it text as a date or is it really a date?
use this if it is text:

(you could get an error with this if the data is copied and you have strange text characters after the year.)
Excel Formula:
=Date(textafter(H2,"/",2),textbefore(textafter(H2,"/"),"/"),left(H2,2))

use this if H is really a date:
Excel Formula:
=Date(2024,Month(H2),Day(H3))
 
Upvote 0
Assuming it is entered as a valid date on your sheet, here is a simple example to show how you can pull the date from cell H1 and convert it to the current year dynamically.
VBA Code:
Sub DateUpdate()

    Dim dt1 As Date
    Dim dt2 As Date
    
'   Get date from cell H1
    dt1 = Range("H1")
    
'   Convert date to current year
    dt2 = DateSerial(Year(Date), Month(dt1), Day(dt1))
    
'   Display new date
    MsgBox dt2
    
End Sub
 
Upvote 0
Assuming it is entered as a valid date on your sheet, here is a simple example to show how you can pull the date from cell H1 and convert it to the current year dynamically.
VBA Code:
Sub DateUpdate()

    Dim dt1 As Date
    Dim dt2 As Date
  
'   Get date from cell H1
    dt1 = Range("H1")
  
'   Convert date to current year
    dt2 = DateSerial(Year(Date), Month(dt1), Day(dt1))
  
'   Display new date
    MsgBox dt2
  
End Sub
Thanks.
But I get a debug error at

' Get date from cell H1
dt1 = Range("H1")
 
Upvote 0
Do you need VBA?

Is it text as a date or is it really a date?
use this if it is text:

(you could get an error with this if the data is copied and you have strange text characters after the year.)
Excel Formula:
=Date(textafter(H2,"/",2),textbefore(textafter(H2,"/"),"/"),left(H2,2))

use this if H is really a date:
Excel Formula:
=Date(2024,Month(H2),Day(H3))
It is VBA code I need for this mate, not a formula.
 
Upvote 0
Thanks.
But I get a debug error at

' Get date from cell H1
dt1 = Range("H1")
Do you have a valid date in cell H1?

If you enter this formula anywhere in a blank cell, what does it return?
Excel Formula:
=ISNUMBER(H1)
 
Upvote 0
Do you have a valid date in cell H1?

If you enter this formula anywhere in a blank cell, what does it return?
Excel Formula:
=ISNUMBER(H1)
Ahh. H1 has the title of date. The dates then run from H2 onwards dynamically.
When using =ISNUMBER(H2) I get a TRUE result.
 
Upvote 0
OK, so if you substitute H2 for H1 in that code, it should work.
 
Upvote 0
If you want it to update all the rows from row 2 to the last row with data in column H, then as long as every one of those rows has a valid date, this code will update all of them at once.
VBA Code:
Sub DateUpdate()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Find last row with data in column H
    lr = Cells(Rows.Count, "H").End(xlUp).Row
   
'   Loop through all rows starting with row 2
    For r = 2 To lr
'       Update to current year
        Cells(r, "H").Value = DateSerial(Year(Date), Month(Cells(r, "H")), Day(Cells(r, "H")))
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Solution
If you want it to update all the rows from row 2 to the last row with data in column H, then as long as every one of those rows has a valid date, this code will update all of them at once.
VBA Code:
Sub DateUpdate()

    Dim lr As Long
    Dim r As Long
  
    Application.ScreenUpdating = False
  
'   Find last row with data in column H
    lr = Cells(Rows.Count, "H").End(xlUp).Row
  
'   Loop through all rows starting with row 2
    For r = 2 To lr
'       Update to current year
        Cells(r, "H").Value = DateSerial(Year(Date), Month(Cells(r, "H")), Day(Cells(r, "H")))
    Next r
  
    Application.ScreenUpdating = True
  
End Sub
Genius. Thanks so much, that's worked perfectly.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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