VBA insert template fails randomly

shippey121

Well-known Member
Joined
Mar 21, 2006
Messages
1,003
Hi all,

i have a sheet where the users type in summary information, after all data is entered they run a code which creates several new sheets from a template where the user then enters more data,

the problem im having is that the program randomly crashes with error code 1004 on this line

[/code]Sheets.Add After:=Worksheets(shts), Type:="c:\check sheet template.xlt"
Code:
for example if the summary sheet contains 20 items the code runs fine and inserts 20 copies of the template, it seems to become a problem when trying to insert 100+ sheets.

i dont think its the pc as ive tried on several,

the loop that inserts the templates is

x = Range("I200").End(xlUp).Row - 3

For nwsheet = 1 To x
shts = Worksheets().Count - 1

Sheets.Add After:=Worksheets(shts), Type:="c:\check sheet template.xlt"
ActiveSheet.Name = "Sheet " & nwsheet

Next nwsheet

sorry for the poor layout, i forgot the code tags
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Hi andrew,

yes i ran into the problem you suggested before, originally the template was part of the workbook and the code made duplicates of it, this failed, the new code works and usually only fails when there are quite a few to be inserted, but, as mentioned not always.

for the record im using XL2003 but have tried the code on 2007 and 2010 with the same random crashes ??

is there another way i could achieve what im after
 
Upvote 0
You could try saving, closing and reopening the workbook periodically as suggested in the RESOLUTION section of the link I posted.
 
Upvote 0
thanks Andrew, never noticed that line was a link, tried that and after a few adjustments to incorporate it into my code it seems to be working, it does slow the process down but at least it works

thanks
 
Upvote 0
just an update,

the resolution posted in andrews link which was to save, close and reopen the file worked but slowed the job down, this was due to the files being stored on a server and not locally so had to save across the network several time.

i came up with my own work around to the problem which not only works but is considerably faster too.

the template sheets i was inserting consisted of a single sheet, which the code would insert multiple times. i altered the template file to contain as many template sheets as i would need, i then altered my code to open this file and select as many sheets as i require and copy them in one transaction to the new book, the code runs faster now but also the file creation is quicker due to only one file being read.

thanks
 
Upvote 0

Forum statistics

Threads
1,224,386
Messages
6,178,290
Members
452,836
Latest member
knoharra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top