Selecting active workbook - not personal.xls

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.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi and welcome to the board.

Try changing the first part of your code like this

Rich (BB code):
mainWB = ActiveWorkbook
MyPath = mainWB.Path
For Each sht In mainWB.Sheets
 
Upvote 0
Hi Dave,

Thank you for your help.

I've tried that and am getting run-time error '438'
Object doesn't support this property or method




Hi and welcome to the board.

Try changing the first part of your code like this

Rich (BB code):
mainWB = ActiveWorkbook
MyPath = mainWB.Path
For Each sht In mainWB.Sheets
 
Upvote 0
Hi

An alternative might be just to grab the workbooks name and use it in the latter part of the routine.

Code:
mainWB = ActiveWorkbook.Name
MyPath = Workbooks(mainWB).Path
For Each sht In Workbooks(mainWB)
...
 
Upvote 0
Sorry, I forgot to also copy the .Sheets in the For Each loop, does this rectify the issue or do you get the same error?
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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