VBA/Macro to select specific worksheets and save them as separate files

thechad

Board Regular
Joined
Apr 28, 2014
Messages
117
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Greetings,

I found this snippet of code in a Mr Excel post from 2018 that seems simple enough however I continue to receive the error below. Not sure what the issue is but would appreciate any assistance.

VBA Code:
Sub SaveSheetsAsFiles()

Dim SheetsToSave
SheetsToSave = Array("407 & HALTON", "DURHAM", "HAMILTON") 'change tab names to suit

Application.ScreenUpdating = False

For Each sht In Sheets(SheetsToSave)
    sht.Copy
    'file location is same as workbook the code is in. Change to suit
    'file format is .xlsm - change to suit
    ActiveWorkbook.SaveAs Filename:=sht.Name & ".xlsx", FileFormat:=52

Next sht

Application.ScreenUpdating = True

End Sub


ERROR:
Run-time error '1004':

This extension cannot be used with the selected file type. Change the file extension in the File name text box or select a different file type by changing the Save as type.


It will generate one of the pages and then the error arises.

Thanks in advance!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi there,

For xlsx files the FileFormat is 51 not 52 (these are for xlsm files) as you have it.

Regards,

Robert
 
Upvote 1
Solution
Yessssss......you are right! I vaguely remember something about that in days gone by. Thank you! As I have had some time to play with other stuff that I found that didn't do what I was looking for, I am noticing that it is copying everything, including the formulas. Is there a way to copy the values/formats and ditch the formulas in the new versions?

C
 
Upvote 0
Robert...I was able to cobble together a solution but would've gone mad without your assistance. Thanks again!!!!!!!
 
Upvote 0
Robert...I was able to cobble together a solution but would've gone mad without your assistance. Thanks again!!!!!!!

You're welcome 👍 Glad we got it sorted 😎
 
Upvote 1

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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