S
Simon Lloyd
Hi all, I have the code below that copies all visible WorkSheets to a
new folder and renames the sheets, however i am struggling with the
fact that if the folder exists the code stops and shows a Path error,
how can i modify the code to check if folder exists, if it does just
copy the worksheet with the DateString in to that folder?
All help greatly appreciated!
Regards,
Simon
Sub Copy_All_Visible_Sheets_To_New_Workbook()
Dim WbMain As Workbook
Dim Wb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
Ash = ActiveSheet.Name
Application.ScreenUpdating = False
Application.EnableEvents = False
DateString = Format(Now, "dd-mm-yyyy")
Set WbMain = ThisWorkbook
FolderName = WbMain.Path & "\" & Left(Ash, Len(Ash) - 0)
MkDir FolderName
For Each sh In WbMain.Worksheets
If sh.Visible = -1 Then
sh.Copy
Set Wb = ActiveWorkbook
Wb.SaveAs FolderName _
& "\" & Wb.Sheets(1).Name & " " & DateString &
".xls"
Wb.Close False
End If
Next sh
MsgBox "Look in " & FolderName & " for the files"
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
new folder and renames the sheets, however i am struggling with the
fact that if the folder exists the code stops and shows a Path error,
how can i modify the code to check if folder exists, if it does just
copy the worksheet with the DateString in to that folder?
All help greatly appreciated!
Regards,
Simon
Sub Copy_All_Visible_Sheets_To_New_Workbook()
Dim WbMain As Workbook
Dim Wb As Workbook
Dim sh As Worksheet
Dim DateString As String
Dim FolderName As String
Ash = ActiveSheet.Name
Application.ScreenUpdating = False
Application.EnableEvents = False
DateString = Format(Now, "dd-mm-yyyy")
Set WbMain = ThisWorkbook
FolderName = WbMain.Path & "\" & Left(Ash, Len(Ash) - 0)
MkDir FolderName
For Each sh In WbMain.Worksheets
If sh.Visible = -1 Then
sh.Copy
Set Wb = ActiveWorkbook
Wb.SaveAs FolderName _
& "\" & Wb.Sheets(1).Name & " " & DateString &
".xls"
Wb.Close False
End If
Next sh
MsgBox "Look in " & FolderName & " for the files"
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub