PC Review


Reply
Thread Tools Rate Thread

Adding a sheet to a workbook

 
 
Paul
Guest
Posts: n/a
 
      24th Mar 2010
I have a Development Notes template that I want to add to the front of larger
models that I build.

Having saved the template as a file, I have code that is run from an icon
that should
- identify the activeworkbook
- open the template file
- copy the notes sheet into the activeworkbook
- close the template file

It keeps failing on the second of these two lines, with a 'Subscript out of
range' error

cSheet = Workbooks(cMainWin).Sheets(1).Name
Workbooks(cNotesFile).Worksheets(cNotesFile).Copy
Before:=Workbooks(cMainWin).Sheets(cSheet)

' cNotesFile is the name of the notes template
' cMainWin is the name of the activeworkbook

The first line attempts to get the name of the first sheet in the file (I
originally tried using Sheet(1) but with the same error)

This should be straightforward, but clearly I'm making it more difficult
than it should be - and making it fail at the same time.

Any help would be appreciated

--
If the post is helpful, please consider donating something to an animal
charity on my behalf .......... and click Yes
 
Reply With Quote
 
 
 
 
john
Guest
Posts: n/a
 
      25th Mar 2010
something like following should do what you want.

Sub CopyWorksheetFromTemplate()
Dim wb1 As Excel.Workbook
Dim wb2 As Excel.Workbook
Dim TemplateFile As String
Dim FolderName As String

TemplateFile = "cNotesFile.xlt"

FolderName = "C:\Documents and Settings\Paul\Application
Data\Microsoft\Templates\"

Application.ScreenUpdating = False

Set wb1 = ActiveWorkbook 'Workbooks("cMainWin.xls")


On Error Resume Next
Set wb2 = Workbooks(TemplateFile)
On Error GoTo 0

If wb2 Is Nothing Then

Set wb2 = Workbooks.Open(FolderName & TemplateFile, Editable:=True)

Else

Set wb2 = Workbooks(TemplateFile)

End If

wb2.Worksheets(1).Copy Before:=wb1.Sheets(1)

wb2.Close False

Application.ScreenUpdating = True

End Sub
--
jb


"Paul" wrote:

> I have a Development Notes template that I want to add to the front of larger
> models that I build.
>
> Having saved the template as a file, I have code that is run from an icon
> that should
> - identify the activeworkbook
> - open the template file
> - copy the notes sheet into the activeworkbook
> - close the template file
>
> It keeps failing on the second of these two lines, with a 'Subscript out of
> range' error
>
> cSheet = Workbooks(cMainWin).Sheets(1).Name
> Workbooks(cNotesFile).Worksheets(cNotesFile).Copy
> Before:=Workbooks(cMainWin).Sheets(cSheet)
>
> ' cNotesFile is the name of the notes template
> ' cMainWin is the name of the activeworkbook
>
> The first line attempts to get the name of the first sheet in the file (I
> originally tried using Sheet(1) but with the same error)
>
> This should be straightforward, but clearly I'm making it more difficult
> than it should be - and making it fail at the same time.
>
> Any help would be appreciated
>
> --
> If the post is helpful, please consider donating something to an animal
> charity on my behalf .......... and click Yes

 
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
prevent a user from adding any sheet in a workbook Subodh Microsoft Excel Programming 2 7th Apr 2010 02:55 AM
Adding new work sheet to end of workbook Frank Driscoll Microsoft Excel Misc 3 15th Dec 2008 05:47 PM
adding a new sheet to a formulated workbook =?Utf-8?B?bWF0dHNob3BwZQ==?= Microsoft Excel Worksheet Functions 2 1st Feb 2007 10:43 PM
Macro to prevent adding sheet in a workbook. hamad.fatima@gmail.com Microsoft Excel Programming 6 15th Aug 2006 03:49 AM
Excel, adding figures from one cell to a summary sheet or workbook petercoe Microsoft Excel Misc 5 28th Apr 2006 01:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:47 AM.