Set Cell to 1st of Next Month - VBA

jollywood

New Member
Joined
Oct 4, 2011
Messages
40
You all have been a wonderful help so far...thank you!

The last and final step of my workbook is to set the cells within a column to the first of the following month. So, for example, this month, the cells would say "11/1/2011"...in November, the cells would say "12/1/2011"

I have no idea where to even begin and I know it has to be easy.

I'm thinking something along these lines but this only gives the current date:

Code:
Sub DataScrubber()

Dim i As Long
Dim LastRow As Long

LastRow = Range("A65536").End(xlUp).Row
Dim cStartRange As Range
Set cStartRange = Range("Q2", Cells(LastRow, 17))

For i = 1 To LastRow - 1
    cStartRange.Cells.Value = Date
Next i
    

End Sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
=DATE(YEAR(TODAY())+IF(MONTH(TODAY())=12,1,0),MONTH(TODAY())+IF(MONTH(TODAY())=12,-11,1),1)

The formula resolving to: =date(year,month,day)

The first part keeps the current year, unless today's month is December. In that case, 1 year must be added to the current year.

The second part adds one month to the current, unless today's month is December. In that case, 11 months must be DEDUCTED from the current month, yielding Month #1 - January.

The third part is merely the number 1. This always yields the first day of the month.

This logic goes way way way way back to early Lotus. It is possible there is now a more streamlined way. If so, I'd be interested for any other responders to post it. However, this method will always work.
 
Upvote 0
In your code, try changing this line from:
Code:
cStartRange.Cells.[COLOR=Red]Value[/COLOR] = [COLOR=Red]Date[/COLOR]
to something like:
Code:
cStartRange.Cells.[COLOR=Red]Formula[/COLOR] = [COLOR=Red]"=EOMONTH(TODAY(),0)+1"[/COLOR]
    'Use the next line only if you want your dates to be fixed values.
    cStartRange.Value = cStartRange.Value
Hope it helps.
 
Upvote 0
Robyn:
It is possible there is now a more streamlined way. If so, I'd be interested for any other responders to post it.
Is this what you meant?
Code:
=EOMONTH(TODAY(),0)+1

(I don't think an add-in is needed for this but I suppose I could be wrong.)

Hope it helps.
 
Upvote 0
The last and final step of my workbook is to set the cells within a column to the first of the following month. So, for example, this month, the cells would say "11/1/2011"...in November, the cells would say "12/1/2011"
Code:
Set cStartRange = Range("Q2", Cells(LastRow, 17))
First off, to make things easier for you in the future, the quoted code line above can be written like this...

Code:
Set cStartRange = Range("Q2", Cells(LastRow, "Q"))
The second argument to the Cells property call does not need to be a number... you can use the column letter, in quotes, instead.

Now, assuming you want each cell to contain the same first-of-the-month date, you can make the assignment all at once like this...

Code:
cStartRange.Cells.Value = DateSerial(Year(Date), Month(Date) + 1, 1)
This code line replaces the For..Next loop you showed us.
 
Upvote 0
HalfAce:

Is this what you meant?
Code:
=EOMONTH(TODAY(),0)+1

Yes, I suppose it is. But - it is an add on. Microsoft Help advises that if you get the #NAME? error, you must install the Analysis ToolPak add-in. Since I got this error, I have to think whether I want to get the add-in. (Depends on what other goodies it offers...) Thanks for the tip. I'll think about it.
 
Upvote 0
jonmo1:

try

=DATE(YEAR(TODAY()),MONTH(TODAY()+1),1)

I, too, missed that the original post looked for a solution in VBA, and posted a non-VBA formula. In cases where a formula is applicable, the one you suggested works in most months. Assuming the spreadsheet is to be used year round, December presents a problem that must be addressed. In December, the next month is not in today's year. The formula must adjust for that. It must also adjust for the fact that the next month is not the current month +1. That would be month #13, which doesn't exist. The formula I proposed - cumbersome as it is - makes the adjustments. The EOMONTH function suggested by HalfAce looks great. I don't have the add in it requires - but I may get it!
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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