Results 1 to 4 of 4

Macro Update - Save worksheets from 2 workbooks into new file (instead of from 1 workbook)

Threaded View

  1. #1
    Registered User
    Join Date
    02-04-2017
    Location
    Belfast
    MS-Off Ver
    2013
    Posts
    7

    Macro Update - Save worksheets from 2 workbooks into new file (instead of from 1 workbook)

    Hi folks,

    JBeaucaire kindly drafted me the macro below which saves worksheets from Workbook1 provided they had certain criteria in Cell G1. (Criteria being the details in column A of worksheet "Menu")

    I'm currently having a slight issue with the file being very large with so many tabs and I was wondering if it would be possible to copy tabs from Workbook1 & Workbook2 (instead of having all tabs in 1 worksheet) provided they had certain criteria in Cell G1. (Criteria being the details in column A of worksheet "Menu")

    Basically I'm trying to get the following:

    If the tab "Menu" of Worksheet1 has a list of names = "Jim", "Mark", etc. in column A of "Menu" tab of Workbook1, the macro copies all tabs from Worksheet1 & Worksheet2 (that contain "Jim" in Cell G1) into a new Workbook, then copies all tabs from Workbook1 & Workbook2 (that contain "Mark" in Cell G1) into a new Workbook...

    Many thanks for the help guys!


    https://www.excelforum.com/excel-pro...-criteria.html

    Option Explicit
    
    Sub CreateWBsByName()
    Dim fPATH As String, Nm As Range, wbNEW As Workbook, ws As Worksheet, CNT As Long
    
    fPATH = "C:\Path\To\Save\New\Files\"        'remember the final \ in this path string
    
    Application.DisplayAlerts = False           'no alerts, will overwrite existing files
    Application.ScreenUpdating = False          'speed up macro
    
    For Each Nm In ThisWorkbook.Sheets("Menu").Range("A:A").SpecialCells(xlConstants)   'each name in the MENU
        For Each ws In ThisWorkbook.Sheets                              'one sheet at at time
            If ws.Range("G1").Value = Nm.Value Then                     'check each ws G1 cell
                If wbNEW Is Nothing Then                                'create a new workbook is needed
                    ws.Copy
                    Set wbNEW = ActiveWorkbook
                Else                                                    '...or copy into existing new workbook
                    ws.Copy After:=wbNEW.Sheets(wbNEW.Sheets.Count)
                End If
                With ActiveSheet
                    .UsedRange.Value = .UsedRange.Value
                End With
            End If
        Next ws
        If Not wbNEW Is Nothing Then                                    'if wbNEW exists then save it and close
            wbNEW.SaveAs fPATH & Nm.Value & ".xlsx", 51
            wbNEW.Close
            CNT = CNT + 1                                               'count how many new workbooks are saved
            Set wbNEW = Nothing
        End If
    Next Nm
    Application.ScreenUpdating = True                                   'reset the screen
    MsgBox "Done, a total of " & CNT & " workbooks were created"
    End Sub
    Last edited by djro69; 10-17-2017 at 04:59 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to Open Excel file, Update links, and Save
    By harry1013 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-01-2017, 01:17 PM
  2. Replies: 0
    Last Post: 10-03-2016, 10:19 AM
  3. [SOLVED] Macro to save three worksheets from a workbook into a separate file
    By robertguy in forum Excel General
    Replies: 2
    Last Post: 09-09-2015, 04:15 AM
  4. [SOLVED] Macro to save selected worksheets as workbooks in variable location
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-25-2015, 10:32 AM
  5. Replies: 9
    Last Post: 06-24-2013, 04:14 PM
  6. Replies: 5
    Last Post: 03-21-2013, 07:34 AM
  7. [SOLVED] Macro to get worksheets to save to new workbook with OPEN workbooks .PATH & " " as name.
    By NicksDad in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2012, 08:07 PM

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