+ Reply to Thread
Results 1 to 8 of 8

VBA code to split sheets and save as seperate CSV files

Hybrid View

mccrimmon VBA code to split sheets and... 05-20-2008, 06:38 AM
Rick_Stanich This will work with up to 8... 05-20-2008, 12:34 PM
mccrimmon Hi I have tried running... 05-21-2008, 04:19 AM
mccrimmon Hi Can anyone help please?... 05-28-2008, 10:15 AM
sweeteebird change the path 05-28-2008, 11:03 AM
  1. #1
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249

    VBA code to split sheets and save as seperate CSV files

    Hello

    I am wondering if anyone can help.

    I have a spreadsheet that we receive that contains 8 different sheets.

    I am looking to create a macro that splits these 8 different sheets into seperate workbooks and saves them as a CSV file?

    Thanks

    McC
    McCrimmon

  2. #2
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    This will work with up to 8 worksheets, it will name each new CSV workbook by the name of the actual worksheet being moved. It will leave the original workbook intact (As a backup, I did not want to delete sheets from the original workbook.)
    Sub Test1()
        Dim i As Integer
        Dim ws As Worksheet
        Dim CntSheets As Long
    
        Application.DisplayAlerts = False
        For i = 1 To Worksheets.Count
            CntSheets = Worksheets.Count
            'MsgBox CntSheets
            If CntSheets = "8" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "7" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "6" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "5" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "4" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "3" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "2" Then
                Sheets("Sheet" & CntSheets).Move
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
                ActiveWindow.Close
            End If
            If CntSheets = "1" Then
                ActiveWorkbook.SaveAs Filename:="U:\" & ActiveSheet.Name & ".csv", FileFormat:=xlCSV, _
                                      CreateBackup:=False
            End If
        Next i
    EndIt:
        Application.DisplayAlerts = True
    End Sub
    Regards

    Rick
    Win10, Office 365

  3. #3
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249
    Hi

    I have tried running the above code, however, nothing appears to happen?

    Any suggestions?

    Thanks again
    Much appreciated

  4. #4
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249
    Hi

    Can anyone help please?

    Thanks

  5. #5
    Registered User
    Join Date
    05-28-2008
    Posts
    8

    Wink change the path

    you probably don't have a drive U: change the path for the save as command to a valid one for your environment.

    regards,
    SweetEbird

  6. #6
    Forum Contributor mccrimmon's Avatar
    Join Date
    02-19-2004
    Location
    Scotland
    MS-Off Ver
    2003
    Posts
    249
    Hi

    Thanks for getting back to me.

    I have already changed the file path to my own drive and it still does not appear to work?

    Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1