VBA Saving tabs as CVS files based on sheet names and date

Kpersen

New Member
Joined
Jan 29, 2018
Messages
25
Good morning.

I hope some one can help me a piece of VBA that I just can not wrap my head around:

I have a workbook with a number of different sheets where a macro can save each of these sheets to a CVS naming the files using the sheet names + a specific word and the current date and time. There is an exception in the VBA that requires this to look for a sheet named USCS and sae this with a slightly different specific text from the other sheets.

Here is my macro:

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
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
End Sub

The existing macro above saves the USCS sheet as US_DGF_USCS_CARe2_YYYYMMDDHHMM

Now, I have added a new sheet named USCS3 which I need to save with all the same rules except this sheet must be saved as US_DGF_USCS_CARe3_YYYYMMDDHHMM. Meaning the number 3 in the sheet name must be disregarded.
The existing saves the USCS sheet as US_DGF_USCS_CARe2_YYYYMMDDHHMM
There should be no changes to the other sheets.

Can anyone tell me how I add this exception to the above macro?

Thank you so much
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
Code:
If xWs.Name = "USCS" Then
   xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe2_" & Dt, xlCSV, Local:=True
ElseIf xWs.Name = "USCS3" Then
   xWs.SaveAs xDir & "" & "US_DGF_" & Left(xWs.Name, 4) & "_CARe3_" & Dt, xlCSV, Local:=True
Else
   xWs.SaveAs xDir & "" & "US_DGF_" & xWs.Name & "_CARe_" & Dt, xlCSV, Local:=True
End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

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