BretStallwood
New Member
- Joined
- Mar 14, 2016
- Messages
- 4
Hi,
First of all I'm a beginner in VBA, but thank you in advance for any help.
I'm trying to create a macro to split all sheets from an active workbook into separate files. After a bit of a Google I've below the below code which works great for me. However, when I moved the code from the workbook to my Personal.xls it is now trying to run the code on Personal.xls rather than the file I'm running the macro on.
I'd do not want to write the filename into VBA as I want to be able to run it on any filename.
Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xlsx"
ActiveWorkbook.Close SaveChanges:=False
Next sht
End Sub
To test this code I just created and saved a file to my desktop called Test.xlsx, named the sheets "A","B",and "C" and put "QWERTY" in each A1 cell.
First of all I'm a beginner in VBA, but thank you in advance for any help.
I'm trying to create a macro to split all sheets from an active workbook into separate files. After a bit of a Google I've below the below code which works great for me. However, when I moved the code from the workbook to my Personal.xls it is now trying to run the code on Personal.xls rather than the file I'm running the macro on.
I'd do not want to write the filename into VBA as I want to be able to run it on any filename.
Sub Splitbook()
MyPath = ThisWorkbook.Path
For Each sht In ThisWorkbook.Sheets
sht.Copy
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
ActiveWorkbook.SaveAs _
Filename:=MyPath & "\" & sht.Name & ".xlsx"
ActiveWorkbook.Close SaveChanges:=False
Next sht
End Sub
To test this code I just created and saved a file to my desktop called Test.xlsx, named the sheets "A","B",and "C" and put "QWERTY" in each A1 cell.