Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 68
Default Macro to Insert New Worksheet

Hello- I have a workbook that contains a macro that formats each new
worksheet that is added. Before I run the macro, I have to insert & rename
the new worksheet. I'm wondering if I can add to my existing macro, code
that would automatically insert a new worksheet and rename the worksheet. My
worksheet names are months of the year, and my worksheet names are just the
months in ascending order for the current year. I'm sure there is a way to
do this, just don't know what my code would be.

Thanks in advance for any help!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Macro to Insert New Worksheet

One way:

Option Explicit
Sub testme()

Dim TestWks As Worksheet
Dim mCtr As Long
Dim myMonth As String
Dim NextMonth As String

NextMonth = ""
For mCtr = 1 To 12
'xl2002+, IIRC
myMonth = MonthName(mCtr, abbreviate:=False)
'before xl2002
'myMonth = Format(DateSerial(2007, mCtr, 1), "mmmm")

Set TestWks = Nothing
On Error Resume Next
Set TestWks = Worksheets(myMonth)
On Error GoTo 0

If TestWks Is Nothing Then
'this month doesn't exist (yet!)
NextMonth = myMonth
'get out of the loop
Exit For
End If
Next mCtr

If NextMonth = "" Then
MsgBox "All months already exist!"
Else
Worksheets.Add.Name = myMonth
End If

End Sub

Amber wrote:

Hello- I have a workbook that contains a macro that formats each new
worksheet that is added. Before I run the macro, I have to insert & rename
the new worksheet. I'm wondering if I can add to my existing macro, code
that would automatically insert a new worksheet and rename the worksheet. My
worksheet names are months of the year, and my worksheet names are just the
months in ascending order for the current year. I'm sure there is a way to
do this, just don't know what my code would be.

Thanks in advance for any help!


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Macro to Insert New Worksheet

Sub addmonths()

For MonthCount = 1 To 12


Worksheets.Add after:=Sheets(Sheets.Count)

ActiveSheet.Name = MonthName(MonthCount)
Next MonthCount
End Sub


"Amber" wrote:

Hello- I have a workbook that contains a macro that formats each new
worksheet that is added. Before I run the macro, I have to insert & rename
the new worksheet. I'm wondering if I can add to my existing macro, code
that would automatically insert a new worksheet and rename the worksheet. My
worksheet names are months of the year, and my worksheet names are just the
months in ascending order for the current year. I'm sure there is a way to
do this, just don't know what my code would be.

Thanks in advance for any help!

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
Need Macro to create a new worksheet and insert image [email protected] Excel Discussion (Misc queries) 3 March 31st 10 08:18 PM
How to insert a macro button in a worksheet laurlang Excel Discussion (Misc queries) 1 November 2nd 07 02:11 PM
Macro or OLE method to insert worksheet from template workbook Lyndon Rickards Excel Discussion (Misc queries) 0 January 30th 06 08:46 PM
I need a macro that will insert names in an excel worksheet, the . Ocicat Excel Programming 1 November 16th 04 06:04 PM
Macro to insert and name a new worksheet Price Excel Programming 4 December 3rd 03 09:27 PM


All times are GMT +1. The time now is 04:27 PM.

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

About Us

"It's about Microsoft Excel"