Automatically Change the Date

seasmith

New Member
Joined
Jul 6, 2011
Messages
44
I have a spreadsheet where I have the date in cell D2 next to a button. When I click the button I need code that checks to see if it has been a month since that date in cell D2. If it has been a month the date changes to the new date. I know I need to use an If else within this code but I am not quite sure how to get the date in D2 to automatically change if it has been a month. Any suggestions?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This macro should work for you:

Code:
Sub Date_MonthsBetweenDates()
      Dim dBeginDate As Date
      Dim dEndDate As Date
      Dim intMonths As Integer
      ' Beginning date.
      dBeginDate = ActiveSheet.Range("D2").Value
      ' Ending Date.
      dEndDate = Date
      ' Calculate number of months between dates.
      intMonths = ((Year(dEndDate) - Year(dBeginDate)) * 12) + _
         Month(dEndDate) - Month(dBeginDate)
      ' Determine if date needs to be updated.
      If intMonths > 0 Then
      ActiveSheet.Range("D2").Value = Date
      End If
      
   End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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