EDATE problem

0 Agios

Well-known Member
Joined
Feb 22, 2004
Messages
570
Office Version
  1. 365
what i want to do is add a new month, so if the previous cell says Aug 23 the new one should say Sept 23
this is my code and a sample of the page


Private Sub CommandButton1_Click()
ActiveCell.EntireColumn.Select
ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 3).EntireColumn
Selection.Copy
ActiveCell.Offset(0, 1).ColumnWidth = 1
ActiveCell.Offset(0, 2).PasteSpecial Paste:=xlPasteAll
ActiveCell.Offset(0, 0) = "=EDATE(R[-3]C,0)"
End Sub
1693247463449.png
 
Your original line said
VBA Code:
ActiveCell.Offset(0, 0) = "=EDATE(R[-3]C,0)"
The post #2 suggestion was to simply change the EDATE formula
VBA Code:
ActiveCell.Offset(0, 0) = "=EDATE(RC[-3],1)"

Why have you further changed it to
VBA Code:
ActiveCell.Offset(0, 0).Value = "=EDATE(ActiveCell.Offset(-3, 0), 1)"

BTW, there is no need for a zero offset so this would work just as well
VBA Code:
ActiveCell = "=EDATE(RC[-3],1)"
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
How about trying this ?
VBA Code:
Sub CommandButton1_Click()
    Dim LastCol As Long
    Dim startCell As Range
    Dim oldDate As Date
    
'locate last existing columns
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
'set the position to work from
Set startCell = Cells(1, LastCol + 3)

With startCell
    'the narrow column
    .Offset(, -1).ColumnWidth = 1
    'copy previous columns
    .Offset(, -3).Resize(, 2).EntireColumn.Copy
    .PasteSpecial Paste:=xlPasteAll
    'previous date was copied so use it
    oldDate = .Value
    'new date
    .Value = DateAdd("m", 1, oldDate)
    'stop the marching ants
    Application.CutCopyMode = False
    'position the cursor
    .Select
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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