Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Increment Month/Year in "mmm-yy" format

I've written a sub to increment the month to the next one, so that Jan-61 -
Feb-61... Dec-61 - Jan-62, etc, whenever a date is in the cell (value in
the formula bar shows UK-style date format dd/mm/yyyy, but formatted as
"mmm-yy" in the cells). The code is copied below. What's driving me mad is
that it works for years up to 1999, it then changes Dec-99 to Jan-00 Ok, but
then it changes Dec-00 to Jan-04 and thereafter the year "sticks" at 2004,
although the months cycle round OK. I've used F8 to watch each step, but I
can't fathom what's going wrong. I think it may have something to do with
the day, because for dates up to 2000, the formula bar shows 01/mm/yyyy, but
after that shows 04 or 05. Is this some weird date format thing - or a UK/US
date incompatibility? Very grateful if somebody could tell me where my code
is going wrong.

Sub NextMonth()
Dim UsedArea As Range
Dim c As Range
Dim ThisMonth As Integer
Dim ThisYear As Integer
Dim NextMonth As Integer
Dim NextYear As Integer
Set UsedArea = ThisWorkbook.Worksheets("Test Sheet").UsedRange
For Each c In UsedArea.Cells
If IsDate(c.Value) Then
ThisMonth = Mid(c.Value, 4, 2)
ThisYear = Right(c.Value, 4)
End If

If ThisMonth = 12 Then
NextMonth = 1
NextYear = ThisYear + 1
Else
NextMonth = ThisMonth + 1
NextYear = ThisYear
End If

c.Value = Format(NextMonth & "-" & NextYear, "mmm-yy")

Next c
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Increment Month/Year in "mmm-yy" format

First, your using Format() returns a string. XL's parser interprets
Jan-05 as 5 January of the current year.

Using Format() has no effect on what is displayed in the cell - that's
determined completely by the number format and how the parser interprets
the input.

Here's one alternative:

Public Sub NextMonth()
Dim c As Range
For Each c In Sheets("Test Sheet").UsedRange
If IsDate(c.Value) Then
With c
.Value = DateSerial(Year(.Value), Month(.Value) + 1, 1)
End With
End If
Next c
End Sub

In article ,
"Ian Ripsher" wrote:

I've written a sub to increment the month to the next one, so that Jan-61 -
Feb-61... Dec-61 - Jan-62, etc, whenever a date is in the cell (value in
the formula bar shows UK-style date format dd/mm/yyyy, but formatted as
"mmm-yy" in the cells). The code is copied below. What's driving me mad is
that it works for years up to 1999, it then changes Dec-99 to Jan-00 Ok, but
then it changes Dec-00 to Jan-04 and thereafter the year "sticks" at 2004,
although the months cycle round OK. I've used F8 to watch each step, but I
can't fathom what's going wrong. I think it may have something to do with
the day, because for dates up to 2000, the formula bar shows 01/mm/yyyy, but
after that shows 04 or 05. Is this some weird date format thing - or a UK/US
date incompatibility? Very grateful if somebody could tell me where my code
is going wrong.

Sub NextMonth()
Dim UsedArea As Range
Dim c As Range
Dim ThisMonth As Integer
Dim ThisYear As Integer
Dim NextMonth As Integer
Dim NextYear As Integer
Set UsedArea = ThisWorkbook.Worksheets("Test Sheet").UsedRange
For Each c In UsedArea.Cells
If IsDate(c.Value) Then
ThisMonth = Mid(c.Value, 4, 2)
ThisYear = Right(c.Value, 4)
End If

If ThisMonth = 12 Then
NextMonth = 1
NextYear = ThisYear + 1
Else
NextMonth = ThisMonth + 1
NextYear = ThisYear
End If

c.Value = Format(NextMonth & "-" & NextYear, "mmm-yy")

Next c
End Sub


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Increment Month/Year in "mmm-yy" format

Many thanks. As usual, the solution is much easier than I was trying to do
it. I should have twigged the DateSerial function.

"JE McGimpsey" wrote in message
...
First, your using Format() returns a string. XL's parser interprets
Jan-05 as 5 January of the current year.

Using Format() has no effect on what is displayed in the cell - that's
determined completely by the number format and how the parser interprets
the input.

Here's one alternative:

Public Sub NextMonth()
Dim c As Range
For Each c In Sheets("Test Sheet").UsedRange
If IsDate(c.Value) Then
With c
.Value = DateSerial(Year(.Value), Month(.Value) + 1, 1)
End With
End If
Next c
End Sub

In article ,
"Ian Ripsher" wrote:

I've written a sub to increment the month to the next one, so that

Jan-61 -
Feb-61... Dec-61 - Jan-62, etc, whenever a date is in the cell (value

in
the formula bar shows UK-style date format dd/mm/yyyy, but formatted as
"mmm-yy" in the cells). The code is copied below. What's driving me mad

is
that it works for years up to 1999, it then changes Dec-99 to Jan-00 Ok,

but
then it changes Dec-00 to Jan-04 and thereafter the year "sticks" at

2004,
although the months cycle round OK. I've used F8 to watch each step, but

I
can't fathom what's going wrong. I think it may have something to do

with
the day, because for dates up to 2000, the formula bar shows 01/mm/yyyy,

but
after that shows 04 or 05. Is this some weird date format thing - or a

UK/US
date incompatibility? Very grateful if somebody could tell me where my

code
is going wrong.

Sub NextMonth()
Dim UsedArea As Range
Dim c As Range
Dim ThisMonth As Integer
Dim ThisYear As Integer
Dim NextMonth As Integer
Dim NextYear As Integer
Set UsedArea = ThisWorkbook.Worksheets("Test Sheet").UsedRange
For Each c In UsedArea.Cells
If IsDate(c.Value) Then
ThisMonth = Mid(c.Value, 4, 2)
ThisYear = Right(c.Value, 4)
End If

If ThisMonth = 12 Then
NextMonth = 1
NextYear = ThisYear + 1
Else
NextMonth = ThisMonth + 1
NextYear = ThisYear
End If

c.Value = Format(NextMonth & "-" & NextYear, "mmm-yy")

Next c
End Sub




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
counta & countblank for "month-to-date/year-to-date"? magsgundah New Users to Excel 3 October 15th 08 07:02 AM
Functions for "current" & "previous" month to calculate data Priss Excel Worksheet Functions 11 April 15th 08 06:24 PM
Tell me which "season" (Month/Day through Month/Day) a date(Month/Day/Year) falls in (any year)??? misscrf Excel Discussion (Misc queries) 1 December 14th 07 02:59 PM
How do I plot "Month, Year" on the x-axis for prior to 1900? Erin Charts and Charting in Excel 2 June 4th 07 02:34 PM
Working out age from "Day" "Month" "Year" timmyc Excel Worksheet Functions 4 February 5th 06 03:07 PM


All times are GMT +1. The time now is 06:58 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"