Save Workbook in VBA

Drfox3

New Member
Joined
Mar 15, 2011
Messages
7
Although I have worked with Excel several years, I am very new to the VBA community. I was hoping that someone might be able to help me in this. I have some code that, when run, goes into an open workbook and copies each sheet to a seperate workbook. I run into problems when I try to save the workbook (created from the worksheet) as it's own file.
What I want to do is to copy each sheet into a new workbook, then save the workbook as the name of the sheet, then close it and go to the next sheet in the original workbook.
When I run the code I get a Run-time error '438' Object doesn't support this property or method.
Help! I can't figure out what i am doing wrong.
Below is the code -

Code:
Dim Sourcewb As Workbook
Set Sourcewb = ActiveWorkbook
Dim j As Integer, k As Integer
j = Worksheets.Count
For k = 1 To j
With Worksheets(k)
Sheets(k).Select
Application.CutCopyMode = False
Sheets(k).Copy
Dim SaveLocation As String
SaveLocation = "I:\ABT&S\SunGard\Reporting\Advisor Book of Busniess\Division_Reports\Division_Reports_New\"
Dim SaveName As Worksheet
Set SaveName = Sheets(k)
ActiveWorkbook.SaveAs Filename:=SaveLocation & SaveName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False (Where error pops up)
Sourcewb.Activate
End With
Next k
 
Last edited by a moderator:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try

Code:
Dim SaveLocation As String
Dim SaveName As String
Dim j As Integer, k As Integer
SaveLocation = "I:\ABT&S\SunGard\Reporting\Advisor Book of Busniess\Division_Reports\Division_Reports_New\"
j = Worksheets.Count
For k = 1 To j
    Sheets(k).Copy
    SaveName = ActiveSheet.Name
    ActiveWorkbook.SaveAs Filename:=SaveLocation & SaveName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWorkbook.Close
Next k
 
Upvote 0
Holy Moley that worked!
Thank you very much - I have been banging my head on this for 5 hours and could not get around the error 438. I will take a long look at your code and, hopefully, learn something.
Don
 
Upvote 0
This is a really nice and effective macro!!!

Well designed....thank you for the help....I modified slightly to get what I needed it to you.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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