Changing the Year in formatted Date

P

Philosophaie

I would like to change to year portion from 2009 to 2010 in multiple cells.
The cells are formatted as Date with *3/14/2001 selected

for k = 1 to 5
Sheets("Sheet1").Cells(k,2)=Date
if Date.year = 2009 then Date.year=2010
next k

Date.year is incorrect. How do I change and save the date?
 
G

Gary''s Student

Sub dural()
Dim d As Date, s As String
For i = 1 To 30
d = Cells(i, 1).Value
m = Month(d)
dt = Day(d)
s = m & "/" & dt & "/" & "2010"
Cells(i, 1).Value = DateValue(s)
Next
End Sub
 
D

Dave Peterson

If you're really populating those cells with the date a year from today...

Dim NewDate as date
newdate = dateserial(year(date)+1,month(date),day(date))
with worksheets("sheet1").cells(1,2).resize(5,1)
.numberformat = "mm/dd/yyyy"
.value = newdate
end with

It'll work for most dates--not sure what you want to happen if you run this on
Feb 29 of a leap year.
 
R

Rick Rothstein

It is a little unclear from your code exactly what you are trying to do
(Date is a built in function in VB), but assuming you have a real date
reference of some kind, just put it in where I show ThisYearsDate and the
DateAdd function (as structured) will add one year to that date. I show the
output being assigned to a variable named NextYearsDate, but you can output
it anywhere you need to (back into a cell, in a MsgBox, etc.)...

NextYearsDate = DateAdd("YYYY", 1, ThisYearsDate)
 
P

Philosophaie

I am wanting to read the year from a cell who is formatted as a date and has
a date in it. If the year is not 2010 then I want to change it to 2010.
 
R

Rick Rothstein

Try it this way...

With Sheets("Sheet1")
For K = 1 To 5
With .Cells(K, 2)
.Value = DateSerial(2010, Month(.Value), Day(.Value))
.NumberFormat = "m/d/yyyy"
End With
Next
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top