+ 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
mccrimmon Hi Thanks for getting back... 05-28-2008, 12:05 PM
Rick_Stanich Please post a copy of your... 05-29-2008, 03:18 PM
honkycat Better VBA Code? 12-01-2008, 01:59 PM
  1. #1
    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

  2. #2
    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
    McCrimmon

  3. #3
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177
    Please post a copy of your workbook, I may be able to offer more help.
    Regards

    Rick
    Win10, Office 365

  4. #4
    Registered User
    Join Date
    12-01-2008
    Location
    Dallas
    Posts
    1

    Better VBA Code?

    I took the above code and tweaked it a bit:

    Sub SplitBookIntoSheets()
        Dim i As Integer
        Dim ws As Worksheet
        Dim CntSheets As Long
        Dim FileFolder As String
     
        FileFolder = InputBox("Where would you like these saved?", "filename", "C:\Temp\")
        For i = 1 To Worksheets.Count
                CntSheets = Worksheets.Count
                If CntSheets <> "1" Then
                    Sheets(CntSheets).Move
                    ActiveWorkbook.SaveAs FileName:=FileFolder & ActiveSheet.Name & ".csv", FileFormat:=xlCSV
                    ActiveWindow.Close
                Else
                    ActiveWorkbook.SaveAs FileName:=FileFolder & ActiveSheet.Name & ".csv", FileFormat:=xlCSV
                    ActiveWindow.Close
                End If
            Next i
    End Sub
    You have to distinguish the final sheet, because you cannot move a sheet out of a document if it is the only sheet. This also resolves any issues on the naming of the tabs (the prior version required that the sheets be named "Sheet3").

    I believe you weren't getting any error messages because the code suppressed those. I did not include those, because I need to know why something didn't do what I thought it would.
    I hope this helps.
    J
    Last edited by VBA Noob; 12-01-2008 at 02:41 PM.

+ 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