PDA

View Full Version : Solved: Help on changing the year



locoff
07-04-2008, 11:51 AM
I'm looking for code to change the year in my sheet. In cell d21 I have the year displayed as yyyy. I would like for the cell to change automatically when opened on the first day of the new year. Any help would be much appreciated.

Bob Phillips
07-04-2008, 12:01 PM
Private Sub Workbook_Open()
With ThisWorkbook.Worksheets(1)
If .Range("D1").Value <> Year(Date) Then
.Range("D1").Value = Year(Date)
End If
End With
End Sub

mikerickson
07-04-2008, 12:18 PM
Put =TODAY() in the cell and format it "yyyy"

locoff
07-04-2008, 12:29 PM
It didn't work. This is what I copied & pasted:


Private Sub Workbook_Open()
With ThisWorkbook.Worksheets(1)
If .Range("D21").Value <> Year(Date) Then
.Range("D21").Value = Year(Date)
End If
End With
End Sub

I'm new at this, so maybe I did it wrong.

locoff
07-04-2008, 12:32 PM
thanks mikerickson, I appreciate it.

Simon Lloyd
07-04-2008, 01:19 PM
It didn't work. This is what I copied & pasted:


Private Sub Workbook_Open()
With ThisWorkbook.Worksheets(1)
If .Range("D21").Value <> Year(Date) Then
.Range("D21").Value = Year(Date)
End If
End With
End Sub

I'm new at this, so maybe I did it wrong.The formula given by MikeRickson is definately the way to go, xld posted code because thats what you asked for, to use it you need to right click on the green excel icon next to the word "File" on the menubar then choose view code and paste the code given in there, now when opening the workbook the code will function.

locoff
07-04-2008, 06:42 PM
Thanks to all for the help! problem solved! Sorry about the cross posting, didn't know all the rules.

Donnie