Change the date

rakupareek

New Member
Joined
Dec 29, 2023
Messages
34
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have a question that
In Excel sheet row A1 future date inserted
In row B1 calculated no of days from today by formula
I want automatic change in date i.e. A1 when date less than today then in range A1 date to be changed next month same date
Please help to generate VBA code for this automatic changes
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I want automatic change in date i.e. A1 when date less than today then in range A1 date to be changed next month same date
It can be done through a simple formula. All depends on when or at what difference you want it to change?

Just to clarify - Are you using Excel 2016 or have upgraded lately?
 
Upvote 0
I would suggest one of two approaches.
1. Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Whenever you activate the sheet, it will check the date in A1 and change it if necessary.
VBA Code:
Private Sub Worksheet_Activate()
    If Range("A1") < Date And Month(Range("A1")) < 12 Then
        Range("A1") = DateSerial(Year(Range("A1")), Month(Range("A1")) + 1, Day(Range("A1")))
    Else
        Range("A1") = DateSerial(Year(Range("A1")) + 1, 1, Day(Range("A1")))
    End If
End Sub
2. Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Whenever you open the workbook, it will check the date in A1 and change it if necessary.
VBA Code:
Private Sub Workbook_Open()
    With Sheets("Sheet1")
        If .Range("A1") < Date And Month(.Range("A1")) < 12 Then
            .Range("A1") = DateSerial(Year(.Range("A1")), Month(.Range("A1")) + 1, Day(.Range("A1")))
        Else
            .Range("A1") = DateSerial(Year(.Range("A1")) + 1, 1, Day(.Range("A1")))
        End If
    End With
End Sub
You could actually use both methods at the same time if you wish. Both macros take into account if the current month is December. If this is the case, the year is also changed.
 
Upvote 0
Solution
It worked properly in my tests using a dummy workbook. It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
Thanks you
Code works fine
Earlier I was make some mistake in code
Thak you very much Mumps
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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