Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Changing the Year in formatted Date

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Changing the Year in formatted Date

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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Changing the Year in formatted Date

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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Changing the Year in formatted Date

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" wrote in message
...
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?


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Changing the Year in formatted Date

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" wrote in message
...
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?


.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default Changing the Year in formatted Date

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" wrote in message
...
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
column formatted to general - cells keep changing to date? lilsparkdog New Users to Excel 1 September 11th 08 05:48 PM
changing date formatted as general number into date format pghio Excel Programming 4 March 2nd 07 05:17 PM
changing the year of a date zapszipszops Excel Worksheet Functions 2 April 14th 06 04:57 AM
how do i add 1 year to a date formatted cell? rogerm Excel Worksheet Functions 5 September 4th 05 06:46 AM
Changing the year only in a date cs_vision Excel Programming 1 June 8th 05 05:19 PM


All times are GMT +1. The time now is 08:57 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"