Consulting

Results 1 to 5 of 5

Thread: Copy sheet to new workbook

  1. #1

    Question Copy sheet to new workbook

    So, during past days I had several questions and recieved much help here. THANKS . I'm finally getting to the end of my project(automatization of giving contacts to sales reps every morning.) My excel file now downloads SQL data, filters it and spreads it among the sale reps. Each Sale rep has his own sheet. But I dont want them to acces the main file, I'll rather create new excel file for each sale rep. I have sheet names stored in an array, and I would like the new workbooks to have a same name, save the new workbook and close it.
    I know it should be something like this
    For i = lbound(Wnames) to Ubound(Wnames)
    Set NewBook = Workbooks.Add
    NewBook.title = wnames(i) 
    NewBook.saveas = wnames(i) & ".xlxs"
    Activeworkbooks.Sheets(Wnames(i)).move workbooks(Wnames(i))next i
    but I cant get it working...I'l also would like to implement an safe feature, when one of the sheets doesnt exist - but i do not know how.
    Thanks

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. you has ".xlxs" and it should be ".xlsx"

    2. This loops through the worksheets and creates a new workbook for each worksheet, save the WB as the sheet name

    3. I added a automatic delete if the WB exists

    4. I added a date stamp to the WB name (just because I like to) but you can delete that if you don't want

    You'll have to integrate this, changing some things, into your over all project


    Option Explicit
    Sub Macro1()
        Dim ws As Worksheet
        Dim wb1 As Workbook, wb2 As Workbook
        Dim sName As String
        
        Application.ScreenUpdating = False
        
        Set wb1 = ThisWorkbook
        For Each ws In wb1.Worksheets
            ws.Copy
            Set wb2 = ActiveWorkbook
            sName = wb1.Path & Application.PathSeparator & ActiveSheet.Name & Format(Date, "-yyyy-mm-dd") & ".xlsx"
            
            On Error Resume Next
            Kill sName
            On Error GoTo 0
            
            wb2.SaveAs sName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            wb2.Close
        Next
        Application.ScreenUpdating = True
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Works like a charm, what if I would like to overwrite existing files?

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Unless I misunderstand, Paul already included a Kill to delete any existing file.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    See #3 in Post #2
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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