Cannot Find File error when creating multiple workbooks in a loop

rclark

New Member
Joined
Jun 24, 2015
Messages
27
I have a macro that is looping through the creation of a new workbook, copying a sheet to it from the book running the macro, saving and closing the new workbook with a new name.
I am getting inconsistent results as sometimes it works fine but usually I get a VB error "File not found: 'C:\Users\me\AppData\Local\Temp\VBxxx.tmp'
Every time I click "ok" then "debug" then continue playing the macro it progresses.
It may create 2 successful files or 10 before it errors. I'm typically making under 30 files.
I added a wait command in the macro which seems to help, but it still errors out on me. the code alone works, but in a loop I am unsuccessful. Here is the code:

'''''Copy Out Audit Sheet'''''
Application.DisplayAlerts = False
Dim NewBook As Workbook
FPath = Path
FName = CurrFileName
Set NewBook = Workbooks.Add
ThisWorkbook.Sheets("Production 5S").Copy Before:=NewBook.Sheets(1)
NewBook.SaveAs Filename:=FPath & "" & FName
Workbooks(FName).Close
'''''Wait a second to avoid "File Not Found"
Application.Wait Now + #12:00:05 AM#
Application.DisplayAlerts = True
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try changing:
Code:
Workbooks(FName).Close
to:
Code:
NewBook.Close False

Please use CODE tags - the # icon in the message editor. Then this line displays correctly with the back slash:
Code:
NewBook.SaveAs Filename:=FPath & "\" & FName
 
Upvote 0
Try changing:
Code:
Workbooks(FName).Close
to:
Code:
NewBook.Close False

Please use CODE tags - the # icon in the message editor. Then this line displays correctly with the back slash:
Code:
NewBook.SaveAs Filename:=FPath & "\" & FName

Thank you for the response. Unfortunately this didn't improve my issue. It looped twice and then the error popped up.
There is nothing else in the loop referencing external files so I'm sure it's isolated to this part of the code. It feels like a timing issue but I'm a novice user.
 
Upvote 0
One additional note: When it errors, it leaves an unnamed book open in Excel (Bookx) and creates the new sheet but hasn't copied yet.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,822
Members
449,190
Latest member
rscraig11

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