VBAAccountant
New Member
- Joined
- Jun 12, 2018
- Messages
- 15
Hello,
I have an excel file with multiple sheets (Ex: Santa, Rudolf, Dasher, etc) in my Current folder. I need to create a macro that will copy those sheets into seperate existing folders with the same names (Santa, Rudolf, Dasher, etc) in another network folder path.
I have this macro that copies the sheets and saves them as separate files (which I want), but they are all going in the a new folder together. I'm not sure how to get them to be saved individually in the existing folders with the corresponding names:
Sub SaveShtsAsBook()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.Path & "\" & _
Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir MyFilePath '<< create a folder
For N = 5 To Sheets.Count
Sheets(N).Activate
SheetName = ActiveSheet.Name
Cells.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
With .ActiveSheet
.Paste
.Name = SheetName
[A1].Select
End With
'save book in this folder
.SaveAs Filename:=MyFilePath _
& "\" & SheetName & ".xls"
.Close SaveChanges:=True
End With
.CutCopyMode = False
Next
End With
Sheet1.Activate
End Sub
Any suggestions would be greatly appreciated : )
I have an excel file with multiple sheets (Ex: Santa, Rudolf, Dasher, etc) in my Current folder. I need to create a macro that will copy those sheets into seperate existing folders with the same names (Santa, Rudolf, Dasher, etc) in another network folder path.
I have this macro that copies the sheets and saves them as separate files (which I want), but they are all going in the a new folder together. I'm not sure how to get them to be saved individually in the existing folders with the corresponding names:
Sub SaveShtsAsBook()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.Path & "\" & _
Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir MyFilePath '<< create a folder
For N = 5 To Sheets.Count
Sheets(N).Activate
SheetName = ActiveSheet.Name
Cells.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
With .ActiveSheet
.Paste
.Name = SheetName
[A1].Select
End With
'save book in this folder
.SaveAs Filename:=MyFilePath _
& "\" & SheetName & ".xls"
.Close SaveChanges:=True
End With
.CutCopyMode = False
Next
End With
Sheet1.Activate
End Sub
Any suggestions would be greatly appreciated : )