excel saving to xlstart

rikvny02

Board Regular
Joined
Aug 9, 2022
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Can someone help. Why are my sheets saving to the XLstart folder instead of the folder path with the original file. Thanks in advance

Private Sub CreateWorkbooks()

Dim WB As Workbook

Dim ws As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each ws In Worksheets

ws.Copy

Set WB = ActiveWorkbook

WB.SaveAs ThisWorkbook.Path & "\" & ws.Name, FileFormat:=52

WB.Close

Next ws

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Because "ThisWorkbook" refers to the workbook where the VBA code is stored.
Try this instead:
VBA Code:
Private Sub CreateWorkbooks()

Dim WB As Workbook

Dim ws As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False

For Each ws In Worksheets

ws.Copy

Set WB = ActiveWorkbook

WB.SaveAs WB.Path & "\" & ws.Name, FileFormat:=52

WB.Close

Next ws

End Sub
 
Upvote 0
unfortunately the code fails at:

WB.SaveAs WB.Path & "\" & ws.Name, FileFormat:=52
 
Upvote 0
unfortunately the code fails at:

WB.SaveAs WB.Path & "\" & ws.Name, FileFormat:=52
What is the exact error message you are getting?

Also try adding this line just above that, and tell us what it returns:
VBA Code:
MsgBox WB.Path & "\" & ws.Name
 
Upvote 0
I have signed of for the night so hopefully @Joe4 will run with it but you are getting the path from the brand new workbook created by the ws.copy which has never been saved before.
You want to capture the path before going into the loop.
 
Upvote 0
1688997150202.png
the msg is the name of the 1st sheet



Below is the error message
1688996873887.png
 
Upvote 0
Did you see Alex's reply?
What directory are you trying to save the file to?
Where does that come from (what relation is it to any of your files)?
 
Upvote 0
I did not see Alex's reply. The file is saved to a specific folder. H:drive / balances
 
Upvote 0
I did not see Alex's reply. The file is saved to a specific folder. H:drive / balances
So then why not just use:
VBA Code:
WB.SaveAs "H:\balances\" & ws.Name, FileFormat:=52
 
Upvote 0
unfortunately i'm getting the same error. do you think the file could be corrupt?
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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