Save tabs as individual files with exceptions

Kpersen

New Member
Joined
Jan 29, 2018
Messages
25
I am hoping someone can help me solve a puzzle here. I have a workbook with a number of tabs for which I have created macro to save each of these as it's own VBA file. When the files are saved the macro is designed to use the name of the tab combined with a predefined value and a date and time.


Here is my issue. I have only single tab that I wish to exclude, as it needs a different naming convention but I cannot figure out how to exclude it from the macro I have and then get the same macro to save that file with a different naming convention.


Here is what I have:


Public Sub SaveWorksheetsAsCsv222()
Dim xWs As Worksheet
Dim xDir As String
Dim folder As FileDialog
dt = Format(CStr(Now), "YYYYMMDDHHMM")
Set folder = Application.FileDialog(msoFileDialogFolderPicker)
If folder.Show <> -1 Then Exit Sub
xDir = folder.SelectedItems(1)
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & dt, xlCSV, Local:=True
Next
End Sub


What I need is for a tab named USCS to be saved using the same criteria but instead of "CARe: it needs to be CARe2 - Just this tab only.


Can anyone help please?
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try. Adjust "SpecialWorksheet" as needed.
Code:
For Each xWs In Application.ActiveWorkbook.Worksheets
    If xWs.Name <> "SpecialWorksheet" Then
        xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & dt, xlCSV, Local:=True
    End If
Next
 
Upvote 0
Try. Adjust "SpecialWorksheet" as needed.
Code:
For Each xWs In Application.ActiveWorkbook.Worksheets
    If xWs.Name <> "SpecialWorksheet" Then
        xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & dt, xlCSV, Local:=True
    End If
Next

Thank you for you help. I was unable to make this work. The macro instead saved the other worksheets with an incorrect name.
I need the macro to save all the worksheets as single CSV file the only difference is that the worksheet names "USCS" must be have the number 2 after "CARe" in the name like "CARe2" - when I tried to modify this, it doubled the date and time in the file names for the other worksheets.
 
Upvote 0
How about
Code:
For Each xWs In Application.ActiveWorkbook.Worksheets
   If xWs.Name <> "USCS" Then
      xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & dt, xlCSV, Local:=True
   Else
      xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe2_" & dt, xlCSV, Local:=True
   End If
Next xWs
 
Upvote 0
How about
Code:
For Each xWs In Application.ActiveWorkbook.Worksheets
   If xWs.Name <> "USCS" Then
      xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & dt, xlCSV, Local:=True
   Else
      xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe2_" & dt, xlCSV, Local:=True
   End If
Next xWs

Thank you so much. I got it to work
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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