Results 1 to 5 of 5

Thread: Use a Loop to Copy Certain Sets of Sheets to New Workbooks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Use a Loop to Copy Certain Sets of Sheets to New Workbooks

    So here's my brain-killer:

    I need to save a Glossary tab with each of my worksheets (Plant Code Tabs) in this workbook to send out files to managers. I have the code that creates the individual Plant Code files and saves them to my desktop, using the plant codes for the new workbook name. Can anyone give me a hand here with adding a segment to my loop to add the glossary tab to my new workbooks?

    Also, can I use an "else" to only create specific files if I don't want files of every single tab? I'm new to VBA, and haven't really figured out how to declare and specify details with loops yet. Thank you!


    Code:
    Sub SaveShtsAsBook()
        Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
        MyFilePath$ = ActiveWorkbook.Path & "\" & _
        Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 4)
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
             '      End With
            On Error Resume Next '<< a folder exists
            MkDir MyFilePath '<< create a folder
            For N = 1 To Sheets.Count
                Sheets(N).Activate
                SheetName = ActiveSheet.Name
                Sheets(Array("ActiveSheet", "Glossary of Terms")).Copy
                Workbooks.Add (xlWBATWorksheet)
                With ActiveWorkbook
                    With .ActiveSheet
                        .Paste
                        .Name = SheetName
                        [A1].Select
                    End With
                     'save book in this folder
                    .SaveAs Filename:=MyFilePath _
                    & "\" & SheetName & ".xlsx"
                    .Close savechanges:=True
                End With
                .CutCopyMode = False
            Next
        End With
        Sheet1.Activate
    End Sub

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 07-07-2023 at 12:59 PM.

Similar Threads

  1. Replies: 2
    Last Post: 03-09-2015, 11:26 PM
  2. setting loop code to check all named sheets
    By peter renton in forum Excel Help
    Replies: 9
    Last Post: 01-30-2014, 03:01 AM
  3. Replies: 3
    Last Post: 05-14-2013, 03:25 PM
  4. Search word in different workbooks and sheets
    By k0st4din in forum Excel Help
    Replies: 5
    Last Post: 11-29-2012, 10:10 PM
  5. Replies: 2
    Last Post: 11-08-2012, 01:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •