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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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