+ Reply to Thread
Results 1 to 4 of 4

Exporting worksheets to separate workbooks as duplicates vb

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Exporting worksheets to separate workbooks as duplicates vb

    Hi Guys,
    I am using a code to create duplicate workbooks for each page of my master workbook, and send them into a folder. Is there any way I can alter this code to just pick specific worksheets to export to individual workbooks?
    Sub CreateWorkbooks()
    Dim wbDest As Workbook
    Dim wbSource As Workbook
    Dim sht As Object
    Dim strSavePath As String
    Dim r As Long, c As Long, ws As Worksheet
    On Error GoTo ErrorHandler
    
    Application.ScreenUpdating = False
    
    
    strSavePath = "S:\Folder Pathway\Path\Folder Path\"
    
    
    Set wbSource = ActiveWorkbook
    
    
    For Each sht In wbSource.Sheets
    r = sht.Rows.Find("*", , , , xlByRows, xlPrevious).Row
    c = sht.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
    sht.Copy
    Set ws = ActiveSheet
    ws.Range("A1").Resize(r, c).Value = sht.Range("A1").Resize(r, c).Value
    Set wbDest = ActiveWorkbook
    wbDest.SaveAs strSavePath & sht.Name
    wbDest.Close
    Next
    
    Application.ScreenUpdating = True
    
    ErrorHandler:
    
    End Sub
    Thanks all!

  2. #2
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Exporting worksheets to separate workbooks as duplicates vb

    Hi @ JPSIMMON

    you can try this:


    Sub CreateWorkbooks_2()
    Dim wbDest As Workbook, wbSource As Workbook
    Dim sht As Worksheet
    Dim ws As Worksheet
    Dim strSavePath As String
    Dim r As Long, c As Long
    Dim q As Integer
    On Error GoTo ErrorHandler
    Application.ScreenUpdating = False
    strSavePath = "S:\Folder Pathway\Path\Folder Path\"
    Set wbSource = ActiveWorkbook
    For Each sht In wbSource.Worksheets
        q = MsgBox("Create new book for: " + sht.Name, vbYesNo + vbQuestion, "My Process")
        If q = 6 Then
            r = sht.Rows.Find("*", , , , xlByRows, xlPrevious).Row
            c = sht.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
            sht.Copy
            Set ws = ActiveSheet
            ws.Range("A1").Resize(r, c).Value = sht.Range("A1").Resize(r, c).Value
            Set wbDest = ActiveWorkbook
            wbDest.SaveAs strSavePath & sht.Name
            wbDest.Close
    
        End If
    Next sht
    Application.ScreenUpdating = True
    ErrorHandler:
    End Sub
    Barriers are there for those who don't want to dream

  3. #3
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Exporting worksheets to separate workbooks as duplicates vb

    Vicho, that's great. This works well. On top of this:
    Is there any way I can just list the tabs in the code that I want to export, without a msgbox prompt?
    Sub CreateWorkbooks_2()
    Dim wbDest As Workbook, wbSource As Workbook
    Dim sht As Worksheet
    Dim ws As Worksheet
    Dim strSavePath As String
    Dim r As Long, c As Long
    Dim q As Integer
    On Error GoTo ErrorHandler
    Application.ScreenUpdating = False
    strSavePath = "S:\Folder Pathway\Path\Folder Path\"
    Set wbSource = ActiveWorkbook
    For Each sht In wbSource.Worksheets
    'Just list below the sheets I want to export    
    q = MsgBox("Create new book for: " + sht.Name, vbYesNo + vbQuestion, "My Process")
        If q = 6 Then
            r = sht.Rows.Find("*", , , , xlByRows, xlPrevious).Row
            c = sht.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
            sht.Copy
            Set ws = ActiveSheet
            ws.Range("A1").Resize(r, c).Value = sht.Range("A1").Resize(r, c).Value
            Set wbDest = ActiveWorkbook
            wbDest.SaveAs strSavePath & sht.Name
            wbDest.Close
    
        End If
    Next sht
    Application.ScreenUpdating = True
    ErrorHandler:
    End Sub


    Thanks again,
    jp

  4. #4
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Exporting worksheets to separate workbooks as duplicates vb

    Hi @JPSIMMON

    There are many ways to do that, here I show you one.
    First run the code "Sub ListSheets()", it will create aa new sheet, list all the sheets in the book, and place a checkbox for each one.
    Then you check all the tabs you want to save, and the you run code "Sub CreateWorkbooks_2()"

    Sub ListSheets()
    Dim wSht As Worksheet, wCon As Worksheet
    Dim i As Integer
    i = 0
    For Each wSht In Worksheets
        If wSht.Name = "MyControl" Then
            Application.DisplayAlerts = False
            wSht.Delete
            Application.DisplayAlerts = True
        End If
    Next wSht
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "MyControl"
    Set wCon = Worksheets("MyControl")
    For Each wSht In Worksheets
         If wSht.Name <> "MyControl" Then
         i = i + 1
              wCon.Cells(i, 1) = i
              wCon.Cells(i, 2) = wSht.Name
              ActiveSheet.CheckBoxes.Add(Cells(i, 3).Left, _
                                          Cells(i, 3).Top, _
                                          5, 5).Select
              With Selection
                  .Caption = ""
                  .Value = xlOff '
                  .LinkedCell = "D" & i
                  .Display3DShading = False
              End With
         End If
    Next wSht
    wCon.Cells(1, 1).Select
    MsgBox "Check the Sheets you want to save" & vbCrLf & "Then run Macro: CreateBooks", , "Record Sheets"
    End Sub
    Sub CreateWorkbooks_2()
    Dim wbDest As Workbook, wbSource As Workbook
    Dim Sht As Worksheet, ws As Worksheet, wCon As Worksheet
    Dim strSavePath As String
    Dim r As Long, c As Long
    Dim i As Integer, LastRow As Integer
    
    On Error GoTo ErrorHandler
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set wCon = Sheets("MyControl")
    LastRow = wCon.Cells(wCon.Rows.Count, 1).End(xlUp).Row
    strSavePath = "S:\Folder Pathway\Path\Folder Path\"
    Set wbSource = ActiveWorkbook
    
    For i = 1 To LastRow
         Set Sht = Worksheets(wCon.Cells(i, 1).Value)
         If wCon.Cells(i, 4).Value = True Then
    '          r = Sht.Rows.Find("*", , , , xlByRows, xlPrevious).Row
    '          c = Sht.Columns.Find("*", , , , xlByColumns, xlPrevious).Column
    '          Sht.Copy
    '          Set ws = ActiveSheet
    '          ws.Range("A1").Resize(r, c).Value = ws.Range("A1").Resize(r, c).Value
    '          Set wbDest = ActiveWorkbook
    '          wbDest.SaveAs strSavePath & Sht.Name
    '          wbDest.Close
        End If
    Next i
    wCon.Delete
    ErrorHandler:
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub

+ 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. Combine 3 separate workbooks, each with 6 worksheets
    By billycasper in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2014, 07:16 AM
  2. Separate Worksheets into Separate Workbooks
    By adileva in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2013, 01:36 PM
  3. [SOLVED] Copying Worksheets from Multiple Workbooks to a Single Workbook, Separate Worksheets
    By DHartwig35805 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-27-2012, 11:38 AM
  4. Copying Worksheets from Multiple Workbooks to a Single Workbook, Separate Worksheets
    By Abhi_1977 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-27-2012, 11:32 AM
  5. Exporting data from separate workbooks to master workbook
    By therock1986 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-22-2010, 11:43 AM
  6. Save worksheets as separate workbooks
    By mridzuani in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-22-2009, 03:20 PM
  7. Combine Workbooks as separate worksheets
    By SAL in forum Excel General
    Replies: 2
    Last Post: 06-01-2005, 12:05 PM

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