PC Review


Reply
Thread Tools Rate Thread

copy worksheet from previous month and rename to current month

 
 
=?Utf-8?B?RGFuIEUu?=
Guest
Posts: n/a
 
      7th Dec 2005
I have a spreadsheet to track time spent on various projects. I would like
to create a macro (tied to a button) that users can click to copy the
previous months worksheet and rename it to the current month and year.

I.E. I may have a worksheet named "DECEMBER 2005" with current projects
filled in. I would like to copy that sheet and have it renamed to "JANUARY
2006" if I run the macro in Jan of 2006. Similarly, in FEB of 2006 I would
like to copy the JANUARY 2006 sheet and rename to FEBRUARY 2006, and so on.
 
Reply With Quote
 
 
 
 
Brian
Guest
Posts: n/a
 
      7th Dec 2005
Here's a set of simple steps... You can just extract the Month name from
the Now() function.

ActiveSheet.Cells.Select
Selection.Copy
Worksheets.Add
Cells.Select
ActiveSheet.Paste
ActiveSheet.Name = "February"




"Dan E." <Dan E.@discussions.microsoft.com> wrote in message
news:AA9B521C-F2E5-4D8B-88D2-(E-Mail Removed)...
> I have a spreadsheet to track time spent on various projects. I would

like
> to create a macro (tied to a button) that users can click to copy the
> previous months worksheet and rename it to the current month and year.
>
> I.E. I may have a worksheet named "DECEMBER 2005" with current projects
> filled in. I would like to copy that sheet and have it renamed to

"JANUARY
> 2006" if I run the macro in Jan of 2006. Similarly, in FEB of 2006 I

would
> like to copy the JANUARY 2006 sheet and rename to FEBRUARY 2006, and so

on.


 
Reply With Quote
 
Rowan Drummond
Guest
Posts: n/a
 
      8th Dec 2005
This is taken from another project but does something similar. Assumes
the sheet you want to copy is the last (right most) sheet in the workbook.

Sub NewMonthSheet()
Dim lSht As Worksheet
Dim nSht As Worksheet
Dim shName As String

Set lSht = Sheets(Sheets.Count)

If IsDate(lSht.Name) Then

shName = Format(DateAdd("m", 1, lSht.Name), "mmmm yyyy")

On Error Resume Next 'Tests that sheet doesn't already exist
Set nSht = Sheets(shName)
On Error GoTo 0

If nSht Is Nothing Then
lSht.Copy after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = shName
Else
MsgBox "Sheet """ & shName & """ already exists!" _
, vbCritical
End If
Else
MsgBox "Last sheet name does not" & Chr(10) _
& "represent a month!", vbCritical
End If
End Sub

Hope this helps
Rowan

Dan E. wrote:
> I have a spreadsheet to track time spent on various projects. I would like
> to create a macro (tied to a button) that users can click to copy the
> previous months worksheet and rename it to the current month and year.
>
> I.E. I may have a worksheet named "DECEMBER 2005" with current projects
> filled in. I would like to copy that sheet and have it renamed to "JANUARY
> 2006" if I run the macro in Jan of 2006. Similarly, in FEB of 2006 I would
> like to copy the JANUARY 2006 sheet and rename to FEBRUARY 2006, and so on.

 
Reply With Quote
 
=?Utf-8?B?RGFuIEUu?=
Guest
Posts: n/a
 
      8th Dec 2005
Thanks! That was almost 100% what I wanted to do. I wanted the previous
month to be the 2nd sheet in the workbook, and the new one to become the 2nd
sheet. Here is the modified code:

Sub NewMonthSheet()
Dim lSht As Worksheet
Dim nSht As Worksheet
Dim shName As String

Set lSht = Sheets(2)

If IsDate(lSht.Name) Then

shName = Format(DateAdd("m", 1, lSht.Name), "mmmm yyyy")

On Error Resume Next 'Tests that sheet doesn't already exist
Set nSht = Sheets(shName)
On Error GoTo 0

If nSht Is Nothing Then
lSht.Copy after:=Sheets(1)
Sheets(2).Name = shName
Else
MsgBox "Sheet """ & shName & """ already exists!" _
, vbCritical
End If
Else
MsgBox "Last sheet name does not" & Chr(10) _
& "represent a month!", vbCritical
End If
End Sub

"Rowan Drummond" wrote:

> This is taken from another project but does something similar. Assumes
> the sheet you want to copy is the last (right most) sheet in the workbook.
>
> Sub NewMonthSheet()
> Dim lSht As Worksheet
> Dim nSht As Worksheet
> Dim shName As String
>
> Set lSht = Sheets(Sheets.Count)
>
> If IsDate(lSht.Name) Then
>
> shName = Format(DateAdd("m", 1, lSht.Name), "mmmm yyyy")
>
> On Error Resume Next 'Tests that sheet doesn't already exist
> Set nSht = Sheets(shName)
> On Error GoTo 0
>
> If nSht Is Nothing Then
> lSht.Copy after:=Sheets(Sheets.Count)
> Sheets(Sheets.Count).Name = shName
> Else
> MsgBox "Sheet """ & shName & """ already exists!" _
> , vbCritical
> End If
> Else
> MsgBox "Last sheet name does not" & Chr(10) _
> & "represent a month!", vbCritical
> End If
> End Sub
>
> Hope this helps
> Rowan
>
> Dan E. wrote:
> > I have a spreadsheet to track time spent on various projects. I would like
> > to create a macro (tied to a button) that users can click to copy the
> > previous months worksheet and rename it to the current month and year.
> >
> > I.E. I may have a worksheet named "DECEMBER 2005" with current projects
> > filled in. I would like to copy that sheet and have it renamed to "JANUARY
> > 2006" if I run the macro in Jan of 2006. Similarly, in FEB of 2006 I would
> > like to copy the JANUARY 2006 sheet and rename to FEBRUARY 2006, and so on.

>

 
Reply With Quote
 
Rowan Drummond
Guest
Posts: n/a
 
      8th Dec 2005
You're welcome.

Dan E. wrote:
> Thanks! That was almost 100% what I wanted to do. I wanted the previous
> month to be the 2nd sheet in the workbook, and the new one to become the 2nd
> sheet. Here is the modified code:
>
> Sub NewMonthSheet()
> Dim lSht As Worksheet
> Dim nSht As Worksheet
> Dim shName As String
>
> Set lSht = Sheets(2)
>
> If IsDate(lSht.Name) Then
>
> shName = Format(DateAdd("m", 1, lSht.Name), "mmmm yyyy")
>
> On Error Resume Next 'Tests that sheet doesn't already exist
> Set nSht = Sheets(shName)
> On Error GoTo 0
>
> If nSht Is Nothing Then
> lSht.Copy after:=Sheets(1)
> Sheets(2).Name = shName
> Else
> MsgBox "Sheet """ & shName & """ already exists!" _
> , vbCritical
> End If
> Else
> MsgBox "Last sheet name does not" & Chr(10) _
> & "represent a month!", vbCritical
> End If
> End Sub
>
> "Rowan Drummond" wrote:
>
>
>>This is taken from another project but does something similar. Assumes
>>the sheet you want to copy is the last (right most) sheet in the workbook.
>>
>>Sub NewMonthSheet()
>> Dim lSht As Worksheet
>> Dim nSht As Worksheet
>> Dim shName As String
>>
>> Set lSht = Sheets(Sheets.Count)
>>
>> If IsDate(lSht.Name) Then
>>
>> shName = Format(DateAdd("m", 1, lSht.Name), "mmmm yyyy")
>>
>> On Error Resume Next 'Tests that sheet doesn't already exist
>> Set nSht = Sheets(shName)
>> On Error GoTo 0
>>
>> If nSht Is Nothing Then
>> lSht.Copy after:=Sheets(Sheets.Count)
>> Sheets(Sheets.Count).Name = shName
>> Else
>> MsgBox "Sheet """ & shName & """ already exists!" _
>> , vbCritical
>> End If
>> Else
>> MsgBox "Last sheet name does not" & Chr(10) _
>> & "represent a month!", vbCritical
>> End If
>>End Sub
>>
>>Hope this helps
>>Rowan
>>
>>Dan E. wrote:
>>
>>>I have a spreadsheet to track time spent on various projects. I would like
>>>to create a macro (tied to a button) that users can click to copy the
>>>previous months worksheet and rename it to the current month and year.
>>>
>>>I.E. I may have a worksheet named "DECEMBER 2005" with current projects
>>>filled in. I would like to copy that sheet and have it renamed to "JANUARY
>>>2006" if I run the macro in Jan of 2006. Similarly, in FEB of 2006 I would
>>>like to copy the JANUARY 2006 sheet and rename to FEBRUARY 2006, and so on.

>>

 
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
Copy worksheet and rename to next month Jock Microsoft Excel Programming 2 23rd Feb 2009 02:11 PM
Retrieve data for previous 3, 6, 12 month given current month =?Utf-8?B?R0I=?= Microsoft Excel Worksheet Functions 4 19th Jul 2007 11:58 PM
printing Little Current month and Little Next month on Banner when it should little PRIOR month and little Next month. jake_allen10@hotmail.com Microsoft Outlook 0 3rd Nov 2006 07:30 PM
deducting the previous month from the current month =?Utf-8?B?Y2xpdmUgamVua2lucw==?= Microsoft Access Getting Started 8 22nd Dec 2004 12:04 AM
Printing Previous & Next Month's Calendar on Current Month Tom Microsoft Outlook 1 24th Feb 2004 10:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:07 PM.