Consulting

Results 1 to 19 of 19

Thread: Adding Worksheets to an Array

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Adding Worksheets to an Array

    I'm trying to add 2 worksheets to an array.
    I know the first sheetname is "Sheet1" however, the other sheetname is derived from the current month.
    My code to determine it is like this;

    Dim MySht
    Worksheets(MySht).Activate
    The 2nd sheetname is the name of the current month

    How do I put both sheets into an array and copy them to a new Workbook and save it ?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try this in the original WB with the 2 worksheets



    Sub Test()
    
        Worksheets(Array("Sheet1", Format(Now, "mmmm"))).Copy
    
        ActiveWorkbook.SaveAs Filename:= _
            Environ("userprofile") & "\Documents\Sheet1AndMonth.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
    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
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Thanks Paul_Hossler

    Finally figured it out.

    Here's my code:

     
        Dim MySht
        Dim strSaveName As String
        Dim b As String
        MySht = Format(Date, "MMMM")   ' Month
       strSaveName = Worksheets("Sheet1").Range("D1").Value
       Sheets("Sheet1").Select
       Sheets(MySht).Select
       b = MySht
       Sheets(Array("Sheet1", b)).Copy
       ActiveWorkbook.SaveAs strSaveName
    Thanks for the code.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Reduces to
    Sheets(Array("Sheet1", Format(Date, "MMMM"))).Copy
        ActiveWorkbook.SaveAs Worksheets("Sheet1").Range("D1").Value
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Hi mdmackillop Thanks :

    Now this has me stumped.
    The code worked perfectly, until I added formatting and data to sheet1.
    Is there any reason why this workbook now DOES NOT WORK?
    I get an error " Method 'Copy' of object 'Sheets' failed . Any ideas why this error ?
    Attached Files Attached Files

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Using D1 generated an illegal file name

    4/30/2017 2:36:36 AM.xls


    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    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

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Something like
    Sheets(Array("Sheet1", Format(Date, "MMMM"))).Copy
    ActiveWorkbook.SaveAs Format(Worksheets("Sheet1").Range("D1").Value, "dd_mmmm_yyyy") & ".xls"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Hi Guys.

    My problem is that when I run the code on newly created blank sheets, the code works, however, I'm trying to determine what is it that stops the code from working on my sheet1.
    I've removed all the macros and data from my Sheet1, but still I get an error. There's something about my Sheet1 that causes the error.

    Thanks guys.

  9. #9
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    I've isolated the problem to one in which some of the formatting can actually cause the error.
    I get an error " Method 'Copy' of object '_WorkSheet' failed is created.
    I'm using Office 2007 and trying to save an .xls file, but somehow VBA doesn't allow this without a work around.

    I get the error even when I try this simple code:

    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets("Sheet1").Copy
    Any suggestions for a Workaround.








  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Both these work for me on your sample workbook
    Sub test()
    Application.DisplayAlerts = False
    ActiveWorkbook.Sheets("Sheet1").Copy
    ActiveWorkbook.SaveAs Format(Worksheets("Sheet1").Range("D1").Value, "dd_mmmm_yyyy") & ".xls"
    End Sub
    
    
    Sub vbcopy()
    Sheets(Array("Sheet1", Format(Date, "MMMM"))).Copy
    ActiveWorkbook.SaveAs Format(Worksheets("Sheet1").Range("D1").Value, "dd_mmmm_yyyy") & ".xls"
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by simora View Post
    I've isolated the problem to one in which some of the formatting can actually cause the error.
    I get an error " Method 'Copy' of object '_WorkSheet' failed is created.
    I'm using Office 2007 and trying to save an .xls file, but somehow VBA doesn't allow this without a work around.

    I get the error even when I try this simple code:

    Any suggestions for a Workaround.

    The 2 lines work using Excel 2016, Win 10 for me
    ---------------------------------------------------------------------------------------------------------------------

    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

  12. #12
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Go back to the original that works and add one bit of formatting at a time to see if you can figure out what formatting causes it to fail
    ---------------------------------------------------------------------------------------------------------------------

    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

  13. #13
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Hi:

    Are you guys using the ABook1.xls workbook. That is the one that is NOT working for me.
    When I run the code, line by line, I can see the new Book1 created in my Taskbar.
    I cant ever click on it to get it to open, and Excel is showing "Saving" continuously.
    Excel is locked up at that point, and I have to use the Task Manager to get out of it.

    I wish that I could go back to re-doo the formatting, but this sheet has worked in the past and is replicated too many times on different systems.
    Any other ideas ?

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    By any chance do you use Malwarebytes?

    With Creator's Update there was some unforeseen interaction with Excel that had similar symptoms
    ---------------------------------------------------------------------------------------------------------------------

    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

  15. #15
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Yes ! I m using Malwarebytes. Will check on it later as I have to run out for a while.

    THANKS for the input. This is starting to drive me crazier.

  16. #16
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Settings, Protection, Manage Protected Applications

    Unselected Excel (plus others if need be)

    Capture.JPG

    Capture1.JPG

    They'll probably fix it soon (I hope)
    ---------------------------------------------------------------------------------------------------------------------

    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

  17. #17
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,055
    Location
    I see you are getting better (neater) with your circle work Paul.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  18. #18
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by Aussiebear View Post
    I see you are getting better (neater) with your circle work Paul.
    They're round -- your eyes are crooked
    ---------------------------------------------------------------------------------------------------------------------

    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

  19. #19
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Paul_Hossler

    Aussiebear

    mdmackillop



    Just wanted to say thanks to all who chimed in. Had to take a break for a little health issue. Ok now.
    I deleted MalwareBytes and re-installed Office 2007.
    Things now work as expected except for a CDO issue with Win 8.1

Posting Permissions

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