+ Reply to Thread
Results 1 to 13 of 13

Copy all sheets to new individual workbooks

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Copy all sheets to new individual workbooks

    Hi

    I have a master sheet that has around 30 or so tabs, I want to have a macro that will copy each of these (values and formatting only) and save them as individual workbooks and generate the file name based on cells B2 and C2.

    IS this something that can be done?

    thanks in advance

  2. #2
    Registered User
    Join Date
    12-14-2012
    Location
    Maidstone, England
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: Copy all sheets to new individual workbooks

    I have something similar that you can modify:

    Sub copy_QC_file()
    
    
        ActiveWorkbook.Save
    
    '
    'is macro file ..
        workbook1 = ActiveWorkbook.Name
        
    'set folder for copy
        copy_dir$ = Worksheets("Data").Cells(1, 22)
    
    'set name  for copy file
        rl$ = Worksheets("Data").Cells(6, 1)
        name3$ = "RL " & rl$ & "Output"
    
    
    'select worksheets to copy to new file
    
        Sheets(Array("Data", "Graphs")).Select
        Sheets(Array("Data", "Graphs")).Copy
        
        ChDir copy_dir$
        
        Worksheets("Data").Cells(1, 22) = ""
        Worksheets("Data").Cells(6, 1) = ""
         
        ActiveWorkbook.SaveAs Filename:= _
            name3$ & ".xlsx", _
            FileFormat:=xlOpenXMLWorkbook, Password:="", WriteResPassword:="", _
            ReadOnlyRecommended:=False, CreateBackup:=False
        ActiveWorkbook.Close
        
        Workbooks(workbook1).Worksheets("Data").Select
        Range("A1").Select
        
        
    
        End Sub
    Basically, mine copies the sheets named 'Data & Graphs' to a new sheet based on the file name contained in cell 6,1 with a prefix of 'RL' and suffix of 'output'. It also clears these values output, but the bare bones is there. The directory to save is stored in cell 1,22 on the main sheet.
    Last edited by marky9074; 12-17-2012 at 06:49 AM.

  3. #3
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: Copy all sheets to new individual workbooks

    thanks, but I can't see how I can get this to cycle through all my sheets (that may have different names each month)

  4. #4
    Registered User
    Join Date
    12-14-2012
    Location
    Maidstone, England
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: Copy all sheets to new individual workbooks

    So you want all sheets in the workbook, or just selected sheets? If you want selected sheets then I think you would have to absolutely reference them by their tab name, or at least their sheet number in code..

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Copy all sheets to new individual workbooks

    Try this code - Change the highlighted section - Keep this code in the file which contains the sheets which need to be saved -

    Sub save_files()
    Dim wb As Workbook
    Dim i As Long
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    For i = 1 To ThisWorkbook.Worksheets.Count
        With ThisWorkbook.Worksheets(i)
            .Cells.Copy
            Set wb = Workbooks.Add
            wb.Worksheets(1).Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats)
            Application.CutCopyMode = False
            wb.SaveAs ("B:\Test\" & .Range("B2").Value & " " & .Range("C2").Value & ".xlsx")
            wb.Close
        End With
    Next i
    
    MsgBox "Files complete"
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    
    End Sub
    Copy the Excel VBA code
    Select the workbook in which you want to store the Excel VBA code
    Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
    Choose Insert | Module
    Where the cursor is flashing, choose Edit | Paste

    To run the Excel VBA code:
    Choose View | Macros
    Select a macro in the list, and click the Run button
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: Copy all sheets to new individual workbooks

    arlu1201 that works great, apart from it's not copying the formatting across or the graphs that are in the orignal tabs

    edit - I've tried changing it to match another macro I had that alost did the same thing, but it's still only copying the data and not formats or graohs
    Last edited by BillDoor; 12-17-2012 at 09:33 AM.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Try this.
    Sub ExplodeWB()
    Dim wbNew As Workbook 
    Dim ws As Worksheet
    Dim strPath As String
    Dim strFileName As String
    
       strPath = ThisWorkbook.Path
    
       For Each ws In ThisWorkbook.Worksheets
    
          strFileName = ws.Range("B2").Value &  " "& ws.Range("C2").Value
          ws.Copy
    
          Set wbNew = ActiveWorkbook
    
          With wbNew
              .SaveAs strPath &"\" & strFileName
              .Close False
           End With
    
       Next ws
    
    End Sub
    If posting code please use code tags, see here.

  8. #8
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: Copy all sheets to new individual workbooks

    Hi Norie - this is failing at

    .SaveAs strPath &"\" & strFileName

    any idea why
    Also, I've had to change the Vlaue after C2 to "text" as C2 is a date and it didn't like it

    edit - fixed the first part, my sheet 1 had blank values in those cells as it's the front sheet.
    next issue is that this is copying the formulae across, when I just want the values
    Last edited by BillDoor; 12-17-2012 at 10:12 AM.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    How is it failing?

    How is the date formatted?

  10. #10
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: Copy all sheets to new individual workbooks

    Sorry, fixed that bit, I'd editted the last post (it was due to a blank cell)

    the next problem is that it's copying the formulae across, can that be stopped? thanks for your help

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Bill

    In the first post you only wanted to copy the worksheets, no mention of formats or values.

    Anyway to get only values try this.
    
          Set wbNew = ActiveWorkbook
    
          With wbNew
                .Worksheets(1).UsedRange.Copy
                .Worksheets(1).Range("A1").PasteSpecial xlPasteValues
              .SaveAs strPath &"\" & strFileName
              .Close False
           End With
    
       Next ws
    
    End Sub

  12. #12
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Shropshire, England
    MS-Off Ver
    Excel 2007 /10 /13
    Posts
    118

    Re: Copy all sheets to new individual workbooks

    Hi, Norie, that worked fine with one tweak to copy all the cells instead (it was just starting at B2, then copying that to A1, sot the format was all out)

    Thanks for the help, appreciate it (my first post does say values and formatting though )

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Bill

    In brackets.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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