Save [most] sheets in Workbook as CSV file with Sheet Name

I have a file with many sheets and need to save most of them in a file with the name of the sheet.

I need the vba to save the file as csv in a folder where year will change and month will change

so like Z:\foldername\yyyy\09 Sep 15\name of file.csv

where yyyy is Current Year

09 Sep 15 is [09 is month so 01 would be Jan, 02 Feb etc]

So lets say I have 10 sheets but do not want to save Sheets1, Sheet3, Sheet4 and Sheet6

I need to save Sheet2, Sheet5, Sheet7, Sheet8, Sheet9 and Sheet10

So the file path would be       Z:\foldername\2015\09 Sep 15\csvSheet2.csv

I use the below code often but it will not do what I need in my current project which as stated above is to save some of the sheets and to save as csv

Sub MonthFolder()
    Dim sFilename As String
    Dim sPath As String
     
    sFilename = "Test File " 'Set filename
 
    sPath = "C:\ABC\XYZ"       'Set base path"
   
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 'Check for "\"
   
    sPath = sPath & Format(Date, "yyyy")
   
    If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
        Shell "cmd /c mkdir """ & sPath & """"
    End If

    sPath = sPath & Format(Date, "\\m mmm yyyy\\")  
    If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
        Shell "cmd /c mkdir """ & sPath & """"
    End If

 ActiveWorkbook.SaveAs sPath & sFilename & Format(Date - 1, " ddmmmyy") & ".xls"

End Sub
Appreciate your help
JagwarmanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
If you change this line...

ActiveWorkbook.SaveAs sPath & sFilename & Format(Date - 1, " ddmmmyy") & ".xls"

Open in new window


To this..

ActiveWorkbook.SaveAs sPath & sFilename & Format(Date - 1, " ddmmmyy") & ".csv"

Open in new window


Saurabh...
JagwarmanAuthor Commented:
Saurabh Singh Teotia

not sure that will do this.....

So lets say I have 10 sheets but do not want to save Sheets1, Sheet3, Sheet4 and Sheet6

 I need to save Sheet2, Sheet5, Sheet7, Sheet8, Sheet9 and Sheet10
Rgonzo1971Commented:
Hi,

pls try

Sub MonthFolder()
     Dim sFilename As String
     Dim sPath As String
      
     sFilename = "Test File " 'Set filename
   
     sPath = "C:\ABC\XYZ"       'Set base path"
     
     If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 'Check for "\"
     
     sPath = sPath & Format(Date, "yyyy")
    
     If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
         Shell "cmd /c mkdir """ & sPath & """"
     End If

     sPath = sPath & Format(Date, "\\mm mmm yyyy\\")
     If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
         Shell "cmd /c mkdir """ & sPath & """"
     End If
    For Each sh In Array("Sheet2", "Sheet5", "Sheet7", "Sheet8", "Sheet9", "Sheet10")
        Sheets(sh).Copy
        ActiveSheet.SaveAs filename:=sPath & "csv" & sh & ".csv", FileFormat:=xlCSV
        ActiveWorkbook.Close False
    Next
End Sub

Open in new window

Regards
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

JagwarmanAuthor Commented:
I think my spec must have not been specific or clear enough, so sorry for that

I am not saving the file that I am in just some of the sheets from within the file.

the sheets I need to save need to be saved in the path Z:\foldername\2015\09 Sep 15\ [and if year or month changes create year and or month]

The filen ame needs to be the name of the sheet so like csvSheet2.csv

There will be some sheets that I do not want to save

Hope this makes it more clear. Please let me know if it does not.

Many thanks for your help
Roy CoxGroup Finance ManagerCommented:
Try this
Sub MonthFolder()
    Dim oWs As Worksheet
    Dim sFilename As String
    Dim sPath As String

    sFilename = "Test File "    'Set filename

    sPath = "C:\ABC\XYZ"       'Set base path"

    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"    'Check for "\"

    sPath = sPath & Format(Date, "yyyy")

    If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
        Shell "cmd /c mkdir """ & sPath & """"
    End If

    sPath = sPath & Format(Date, "\\mm mmm yyyy\\")
    If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
        Shell "cmd /c mkdir """ & sPath & """"
    End If

    For Each oWs In ThisWorkbook.Worksheets
        Select Case oWs.Name
        Case "Sheet1", "Sheet3", "Sheet4", "Sheet6"
        Case Else
            oWs.Copy    ''/// copies sheet to new workbook
            ''/// saves new workbook as csv
            ActiveWorkbook.SaveAs Filename:=sPath & "csv" & oWs & ".csv", FileFormat:=xlCSV
            ActiveWorkbook.Close False
        End Select
    Next
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JagwarmanAuthor Commented:
Hi Roy

I get

vba method copy of object range failed

??
JagwarmanAuthor Commented:
Ah I think that's because the Sheet names are in Caps. If I change to Non Cap it creates a new Workbook but then I get Object doesn't support this property or method
JagwarmanAuthor Commented:
error is at this point

ActiveWorkbook.SaveAs Filename:=sPath & "csv" & oWs & ".csv", FileFormat:=xlCSV
Rgonzo1971Commented:
change to

ActiveWorkbook.SaveAs Filename:=sPath & "csv" & oWs.Name & ".csv", FileFormat:=xlCSV
Rgonzo1971Commented:
My code with Caps All

Sub MonthFolder()
     Dim sFilename As String
     Dim sPath As String
      
     sFilename = "Test File " 'Set filename
   
     sPath = "C:\ABC\XYZ"       'Set base path"
     
     If Right(sPath, 1) <> "\" Then sPath = sPath & "\" 'Check for "\"
     
     sPath = sPath & Format(Date, "yyyy")
    
     If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
         Shell "cmd /c mkdir """ & sPath & """"
     End If

     sPath = sPath & Format(Date, "\\mm mmm yyyy\\")
     If Dir(sPath, vbDirectory) = "" Then             'If folder doesn't exist, create it
         Shell "cmd /c mkdir """ & sPath & """"
     End If
    For Each sh In Array("SHEET2", "SHEET5", "SHEET7", "SHEET8", "SHEET9", "SHEET10")
        Sheets(sh).Copy
        ActiveSheet.SaveAs filename:=sPath & "csv" & sh & ".csv", FileFormat:=xlCSV
        ActiveWorkbook.Close False
    Next
End Sub

Open in new window

JagwarmanAuthor Commented:
Both equally as good. many thanks for your help with this
Roy CoxGroup Finance ManagerCommented:
Glad that we helped.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.