+ Reply to Thread
Results 1 to 10 of 10

Create Separate Workbook for Each Excel Tab

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-15-2015
    Location
    Orlando, FL
    MS-Off Ver
    Office Standard 2016
    Posts
    266

    Create Separate Workbook for Each Excel Tab

    Hello,

    I'm using the following code to make a separate excel workbook for each tab in my main file. Code works great, the only change I'm trying to accomplish would be to have it skip my data tabs. For example, the tabs named "START", "PM FEES", "TEMPLATE", "RECQ FEE", "LEDGER" should not have a separate excel file created. Only if the name is not one of these listed sheet names, then it should create its own workbook when this macro is ran. Can something help me accomplish this change?

    Sub copysheet()
        Dim ws As Worksheet
        Application.DisplayAlerts = False
        For Each ws In ThisWorkbook.Worksheets
            ws.Copy
            wb_name = ws.Name
            ActiveWorkbook.SaveAs Filename:= _
                "T:\Test\Test\Collaboration\FA\Project Financial Summary (DRM)\Project Financial Sheets\" & wb_name & " Project Financial Summary " & Format(Date, "mm-dd-yy") & ".xlsx", FileFormat:=51
            ActiveWorkbook.Close
        Next ws
        Application.DisplayAlerts = True
    End Sub

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Create Separate Workbook for Each Excel Tab

    Does this work ?

    Sub copysheet()
        Dim ws As Worksheet
        Application.DisplayAlerts = False
        For Each ws In ThisWorkbook.Worksheets
    	If ws.name <> "Start" or "PM Fees" or "Template", or "Recq Fee" or "Ledger" Then
            	ws.Copy
            	wb_name = ws.Name
            	ActiveWorkbook.SaveAs Filename:= _
                	"T:\Test\Test\Collaboration\FA\Project Financial Summary (DRM)\Project Financial Sheets\" & wb_name & " 		Project Financial Summary " & Format(Date, "mm-dd-yy") & ".xlsx", FileFormat:=51
            	ActiveWorkbook.Close
    	End If
        Next ws
        Application.DisplayAlerts = True
    End Sub

  3. #3
    Forum Contributor
    Join Date
    05-15-2015
    Location
    Orlando, FL
    MS-Off Ver
    Office Standard 2016
    Posts
    266

    Re: Create Separate Workbook for Each Excel Tab

    I'm receiving an error on the "if ws.name <> "start" line.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Create Separate Workbook for Each Excel Tab

    Edit the path to match your system :

    Option Explicit
    
    Sub SveShts()
    
    Dim xPath As String
    Dim xWs As Worksheet
    
    xPath = Application.ActiveWorkbook.Path
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
        For Each xWs In ThisWorkbook.Sheets
            If xWs.Name <> "Start" And xWs.Name <> "PM Fees" And xWs.Name <> "Template" And xWs.Name <> "Recq Fee" And xWs.Name <> "Ledger" Then
                xWs.Copy
                Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
                Application.ActiveWorkbook.Close False
            End If
        Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-15-2015
    Location
    Orlando, FL
    MS-Off Ver
    Office Standard 2016
    Posts
    266

    Re: Create Separate Workbook for Each Excel Tab

    Now errors out on "xWs.Copy"

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Create Separate Workbook for Each Excel Tab

    Are there other sheets in the workbook besides the ones listed NOT to be copied ?

    Did you edit the path where the workbooks will be saved, to match your computer ?
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    05-15-2015
    Location
    Orlando, FL
    MS-Off Ver
    Office Standard 2016
    Posts
    266

    Re: Create Separate Workbook for Each Excel Tab

    Yes, several other sheets in the workbook. That is why I was needing the statement adding to prevent it from creating workbooks for these specified tabs. It works as intended in the original code i posted, I just wanted to try and avoid it from creating the sheets I don't actually need as a separate workbook.

  8. #8
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Create Separate Workbook for Each Excel Tab

    Nice Coding ... (1 suggestion)

    It's great the way Logit has put it all together ... if you don't mind to much just want to suggest an alternative to the if statement that's a tad for configurable
    Again ... Logit did a verrry nice job
    Note: My suggestion is meant as a alternative and NOT an improvement

    ' just below  Dim Statements 
    Const EXCLUDE_LIST As String = "START,PM Fees,Template,Recq Fee,Ledger"
    
    ' loop for copiing sheets to new books
    If InStr(EXCLUDE_LIST, xWs.Name) = 0 Then
    
      ' ONLY SHEETS NOT IN EXCLUSTION LIST GET HERE 
     
    End If
    Last edited by nimrod1313; 08-23-2022 at 04:55 PM.

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,021

    Re: Create Separate Workbook for Each Excel Tab

    Based on the information you provided, my last code example works. Download the sample workbook and try it. Again, you'll need to edit the path where the books will be saved so it matches
    your computer.

    Change this line :

    Application.ActiveWorkbook.SaveAs Filename:=xPath & "" & xWs.Name & ".xlsx"

    To this :

    ActiveWorkbook.SaveAs Filename:="T:\Test\Test\Collaboration\FA\Project Financial Summary (DRM)\Project Financial Sheets" & xWs.Name & " Project Financial Summary " & Format(Date, "mm-dd-yy") & ".xlsx", FileFormat:=51

  10. #10
    Forum Contributor
    Join Date
    05-15-2015
    Location
    Orlando, FL
    MS-Off Ver
    Office Standard 2016
    Posts
    266

    Re: Create Separate Workbook for Each Excel Tab

    I'm not sure what the issue is but the "xWs.Copy" line still errors out. I appreciate the help, but I'll just stick with my original script and deal with the extra excel files created. Thank you for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Create separate workbooks from a master workbook
    By chris1979 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-06-2022, 10:00 PM
  2. Create variable number of userforms in separate workbook
    By Eatmybrain in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2020, 10:57 AM
  3. Need to create a drop-down menu that lists ALL the sheets on a SEPARATE workbook.
    By IT_Padawan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2018, 04:51 AM
  4. Create a separate workbook using data from another workbook.
    By macro_noob3030 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2017, 10:54 AM
  5. Replies: 0
    Last Post: 02-04-2015, 01:27 PM
  6. Create Separate Workbook and Move any previous versions
    By cmore in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2014, 08:23 AM
  7. [SOLVED] Create pivottable in separate workbook with ptcache
    By Martin Los in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-06-2005, 08:05 AM

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