Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Using VBA to save as (export) specific worksheets

0

Hello, I have a workbook with 5 tabs listed below:

- Dashboard

- Compare

- ADT file

- BEFORE

- AFTER

I would like to be able to write code that loops through each of the 5 worksheets but only save 3 of them to a new file location in a new workbook. The 3 worksheets that i would like to have saved into new workbook are 'Compare', 'BEFORE', and 'AFTER'. This is the code I have so far:

Application.DisplayAlerts = False
Dim worksheet_list As Variant     'this may not apply
Dim wb As Workbook
Dim Path As String
Dim Fname As String
Dim ws As Worksheet
Dim wsdash As Worksheet
Dim wsADT As Worksheet
Dim MacroSheet As Worksheet
worksheet_list = Array("Compare", "BEFORE", "AFTER")    ' this may not apply' 
Set wb = ThisWorkbook
Set ws = wb.Sheets("BEFORE")
Set wsdash = Worksheets("Dashboard")
Set wsADT = Worksheets("ADT File")
Path = "G:\Cayton Files\Excel Macro\"
Fname = ws.Range("A2") & " " & "Restriction APU Review " & Format(Now(), "YYYY-MM-DD") & ".xlsx"
Workbooks.Add
For Each MacroSheet In wb.Sheets

    If MacroSheet.Name <> wsdash.Name Or wsADT Then

        MacroSheet.Copy Before:=ActiveWorkbook.Sheets("Sheet1")

    End If

Next MacroSheet
ActiveWorkbook.SaveAs Filename:=Path & Fname
ActiveWorkbook.Close
Application.DisplayAlerts = True

The error message occurs when i get to this statement: If MacroSheet.Name <> wsdash.Name Or wsADT Then

That's where im stuck. I found a way to just save the entire new workbook (with all 5 sheets) and then just delete the sheets i dont want, however i feel like there may be a more efficient way of doing this (ex: looping through the 5 and only pulling the 3 that i need).

So my question is, how would looping through each of the 5 sheets and only saving the 3 sheets i want into a new workbook work?

Any help is greatly appreciated!

Answer
Discuss

Discussion

Please see Revision 25 April 2024 to my Answer below (and a demo file)
John_Ru (rep: 6152) Apr 25, '24 at 7:13 am
Add to Discussion

Answers

0
Selected Answer

Hi cgreen16

Using an Or in the If statement needs two separate True/False tests. If you want to skip two sheets however, the same is true but the tests need an And not an Or (so the sheet name isn't one thing and isn't the other). Change your line to read this (changes in bold):

If MacroSheet.Name <> wsdash.Name And  MacroSheet.Name<>wsADT.Name Then

Revision 25 April 2024

In the attached file, I added your code to but corrected and modified it (e.g. to remove unnecesary declarations and add comments). Changes in bold below...

Sub CopySheetsButSkipNamed()

    Dim wb As Workbook
    Dim Path As String
    Dim Fname As String
    Dim ws As Worksheet, wsdash As Worksheet, wsADT As Worksheet, MacroSheet As Worksheet

    Application.DisplayAlerts = False

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("BEFORE")
    Set wsdash = Worksheets("Dashboard")
    Set wsADT = Worksheets("ADT File")

    Path = "G:\Cayton Files\Excel Macro\"
    Fname = Trim(ws.Range("A2")) & " " & "Restriction APU Review " & Format(Now(), "YYYY-MM-DD") & ".xlsx"

    Workbooks.Add
    For Each MacroSheet In wb.Sheets
        ' ### corrected line...
        If MacroSheet.Name <> wsdash.Name And MacroSheet.Name <> wsADT.Name Then
            MacroSheet.Copy Before:=ActiveWorkbook.Sheets("Sheet1")
        End If
    Next MacroSheet

    'optional line...
    ActiveWorkbook.Worksheets("Sheet1").Delete
    ' ### restore the following line for real use...
    'ActiveWorkbook.SaveAs Filename:=Path & Fname
    ' ### delete the following line for real use...
    MsgBox "Displayed file NOT YET saved as " & Path & Fname

    ActiveWorkbook.Close
    Application.DisplayAlerts = True

End Sub

If you run this, you'll just get a message box but you can restore the save line for real use.

Hope this works for you. If so please remember to mark this Answer as Selected 

Discuss

Discussion

Thank you! this worked...Appreciate the explanation as well...definitely helped!
cgreen16 (rep: 6) Apr 29, '24 at 11:53 pm
Glad that worked for you. Thanks for selecting my Answer,  cgreen16. 
John_Ru (rep: 6152) Apr 30, '24 at 1:04 am
Add to Discussion
0

Hello cgreen16,

I've only given this a quick lookover, but this is what I would suggest:

You may want to try changing the line:

If MacroSheet.Name <> wsdash.Name Or wsADT Then

to:

If MacroSheet.Name <> "Dashboard" Or "ADT file"

If this solves things for you please mark my answer as Selected; if not, let me know and I will take a deeper look.

Cheers   :-)

Discuss

Discussion

@Willie You did take a quick look and give an answer (thanks) but missed the telling things. I think my Answer should fix it for the user. 
John_Ru (rep: 6152) Apr 24, '24 at 1:04 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login