Hi,
I have some VBA code where I create a new workbook for the active sheet and save that individual sheet to a file a close it.
It works as expected the first time I run it but if the file exists and I run the macro again I get a run time error.
Have done a bit of looking online and found things to try and no one them have solved the issue.
Any help would be appreciated.
With 'Application.DisplayAlerts = False' the error I get is "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed"
With 'Application.DisplayAlerts = True' the error I get is "Run-time error '1004': Cannot access read-only document 'filename.xls'"
The code is below
I have some VBA code where I create a new workbook for the active sheet and save that individual sheet to a file a close it.
It works as expected the first time I run it but if the file exists and I run the macro again I get a run time error.
Have done a bit of looking online and found things to try and no one them have solved the issue.
Any help would be appreciated.
With 'Application.DisplayAlerts = False' the error I get is "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed"
With 'Application.DisplayAlerts = True' the error I get is "Run-time error '1004': Cannot access read-only document 'filename.xls'"
The code is below
Code:
Sub Save_All_As_Sheets()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim WshtNames As Variant
Dim WshtNameCrnt As Variant
Dim MySheet As Worksheet
WshtNames = Array("1", "2")
For Each WshtNameCrnt In WshtNames
With Worksheets(WshtNameCrnt)
Set MySheet = Worksheets(WshtNameCrnt)
MySheet.Activate
F_Name = Format(Range("G1").Value, "dd mmm")
MName = F_Name & ".xls"
ActiveSheet.Copy
With ActiveSheet.UsedRange
.Copy
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.CutCopyMode = False
ActiveWorkbook.SaveAs FileName:=MName, FileFormat:=xlWorkbookNormal
......Some more code to create sheet
Windows(MName).Close savechanges:=True
Windows("Roster Template - February 2018.xls").Activate
Range("A1").Select
End With
Next WshtNameCrnt
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub