PC Review


Reply
Thread Tools Rate Thread

Changing the Year in formatted Date

 
 
Philosophaie
Guest
Posts: n/a
 
      4th Dec 2009
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?
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      4th Dec 2009
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

--
Gary''s Student - gsnu200909


"Philosophaie" wrote:

> 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?

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      4th Dec 2009
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.



Philosophaie wrote:
>
> 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?


--

Dave Peterson
 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      4th Dec 2009
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)

--
Rick (MVP - Excel)


"Philosophaie" <(E-Mail Removed)> wrote in message
news:11024AC7-5D6A-4AC5-B46D-(E-Mail Removed)...
>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?


 
Reply With Quote
 
Philosophaie
Guest
Posts: n/a
 
      4th Dec 2009
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.

"Rick Rothstein" wrote:

> 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)
>
> --
> Rick (MVP - Excel)
>
>
> "Philosophaie" <(E-Mail Removed)> wrote in message
> news:11024AC7-5D6A-4AC5-B46D-(E-Mail Removed)...
> >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?

>
> .
>

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      4th Dec 2009
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

--
Rick (MVP - Excel)


"Philosophaie" <(E-Mail Removed)> wrote in message
news:11024AC7-5D6A-4AC5-B46D-(E-Mail Removed)...
>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?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain Month & Year from a Text formatted Date field using Left Fu reddy Microsoft Access Queries 2 26th Sep 2008 11:44 PM
column formatted to general - cells keep changing to date? lilsparkdog Microsoft Excel New Users 1 11th Sep 2008 05:48 PM
changing date formatted as general number into date format =?Utf-8?B?cGdoaW8=?= Microsoft Excel Programming 4 2nd Mar 2007 05:17 PM
how do i add 1 year to a date formatted cell? =?Utf-8?B?cm9nZXJt?= Microsoft Excel Worksheet Functions 5 4th Sep 2005 06:46 AM
Figuring fiscal year based on formatted date =?Utf-8?B?Y2hlcm1hbg==?= Microsoft Access 2 1st Jun 2005 01:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:50 PM.