Need to add some code - Save tabs as file but exclude certain tabs

bcurrey

Board Regular
Joined
Aug 11, 2011
Messages
107
Office Version
  1. 365
Platform
  1. MacOS
I have the code below that saves each tab as a new file. However, I have a few tabs that I don't want saved as files. Can someone help me on how to exclude specific tabs? I want to exclude on labeled "Original" and one labeled "Work". Thank you!





Code:
For sht = 1 To ThisWorkbook.Sheets.Count   
   DataCol = 1  'is there a specific column that will have data?
   If Worksheets(sht).Cells(11, DataCol) = "" Then GoTo DoNotSaveThisTab


    MyName_Start = "filepath\filename" & ThisWorkbook.Sheets(sht).Name & " " & Format(Date, "mmddyy")


 Sheets(sht).Select
      ActiveSheet.Copy
    With ActiveWorkbook
        .SaveAs MyName_Start & ".xls", FileFormat:=xlExcel8, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=False
        .Close 0
    End With


Next sht
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,982
Office Version
  1. 365
Platform
  1. Windows
Put everything inside of your For loop inside of an IF clause that checks the sheet name, i.e.
Code:
For sht = 1 To ThisWorkbook.Sheets.Count
    [COLOR=#ff0000]If (sht.Name <> "Original") And (sht.Name <> "Work") Then[/COLOR]
    '   Rest of your current code here
    [COLOR=#ff0000]End If[/COLOR]
Next sht
So what is in red indicates the new code to add.
 
Last edited:
Upvote 0

bcurrey

Board Regular
Joined
Aug 11, 2011
Messages
107
Office Version
  1. 365
Platform
  1. MacOS
Put everything inside of your For loop inside of an IF clause that checks the sheet name, i.e.
Code:
For sht = 1 To ThisWorkbook.Sheets.Count
    [COLOR=#ff0000]If (sht.Name <> "Original") And (sht.Name <> "Work") Then[/COLOR]
    '   Rest of your current code here
    [COLOR=#ff0000]End If[/COLOR]
Next sht
So what is in red indicates the new code to add.


Thanks Joe.


When I ran the code, it gave me an error. It highlighted your 1st row of new code and said "Object required".
 
Upvote 0

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,982
Office Version
  1. 365
Platform
  1. Windows
Issue with the sheet name references. Just set it up like you use later in your code.
Code:
    If (Sheets(sht).Name <> "Original") And (Sheets(sht).Name <> "Work") Then
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Try this.

Code:
Dim sht As Worksheet
Dim MyName_Start As String
Dim I As Long
Dim DataCol As Long

    DataCol = 1  'is there a specific column that will have data?

    For I = 1 To ThisWorkbook.Sheets.Count

        Set sht = ThisWorkbook.Worksheets(I)

        Select Case sht.Name
            Case "Working", "Original"    ' add any more sheets to exclude
                ' do nothing
            Case Else

                If sht.Cells(11, DataCol) <> "" Then    '  GoTo DoNotSaveThisTab

                    MyName_Start = "filepath\filename" & sht.Name & " " & Format(Date, "mmddyy")
                    
                    sht.Copy

                    With ActiveWorkbook
                        .SaveAs MyName_Start & ".xls", FileFormat:=xlExcel8, _
                                Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
                                CreateBackup:=False
                        .Close 0
                    End With

                End If
                
        End Select

    Next I
 
Upvote 0

Forum statistics

Threads
1,191,168
Messages
5,985,057
Members
439,936
Latest member
BSR

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
Top