Macro to set start date to 1st of month?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
Hope you can help with this,

I have data in column C which is start date. now I've tried asking people to always make this the 1st of the month but sometime they are still input the date it started for example 20/08/2017
So I need a macro to correct this as my report are set to look for the 1st.

So what I need is a macro that can look at data each time its put into a cell in Column C, and if its not the 1st if the month change it to the first of that month?

any ideas how to do this?

Thanks

Tony
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
One method might be to pick an arbitrary column (BA in this example) to perform a calculation then put it back to column C, like so...

Code:
Sub Macro1()
Dim lst As Long
lst = Range("C" & Rows.Count).End(xlUp).Row
With Range("BA2:BA" & lst)
    .Formula = "=DATE(YEAR(C2), MONTH(C2), 1)"
    Range("C2:C" & lst) = .Value
    .Clear
End With
End Sub
 
Last edited:
Upvote 0
... look at data each time its put into a cell in Column C, and if its not the 1st if the month change it to the first of that month
I think this should do that for you. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. Copy and Paste the code below into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.
4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  
  Set Changed = Intersect(Target, Columns("C"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If IsDate(c.Value) Then c.Value = c.Value - Day(c.Value) + 1
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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