+ Reply to Thread
Results 1 to 8 of 8

How to save to a new worksheet.

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    How to save to a new worksheet.

    The attached workbook attempts to illustrate a problem I have not been able to solve
    There are 2 data worksheets TAPEBUDGETA and LABELBUDGETA and a UTILITIES sheet.
    The problem I am having shows when the button marked FILE on UserForm1 is clicked. This button fires off code which has the following purpose.
    It creates a new workbook
    It takes a range from three worksheets (only two in the down load) in turn and copies it. It creates worksheet in the new work book and names it. It then pastes the copied data as values. For the second and third sheets it has to create a new worksheet.
    I get an error every time at Set wsB =wbB.Worksheets(2).
    I must have coded wrongly somewhere. I hope that someone can see where and put me right.
    John
    Attached Files Attached Files

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How to save to a new worksheet.

    Are you still on Excel 2010?
    In my version (2016), a new workbook only has one sheet by default, so your code is trying to set wsB as the second sheet in a book with only one worksheet.

    Try
    Please Login or Register  to view this content.
    for sheets after the first one.
    Last edited by Arkadi; 07-27-2020 at 09:23 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to save to a new worksheet.

    I am using Microsoft 365 now but the offending code was written several years ago on probably 2008. Thanks for your interest and suggestion. I will try it and let you know how I get on.
    John

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How to save to a new worksheet.

    Hi John,

    That would be the reason then... your newer version of Excel would start a book with only 1 sheet, while in 2010 you'd get 3 sheets in a new workbook.

    Arkadi

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to save to a new worksheet.

    Arkadi,

    Your code worked and I am now producing new sheets at will!
    Many thanks for your insight.
    John

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: How to save to a new worksheet.

    My pleasure John! Thanks for the feedback, the rep, and marking the thread as closed Happy coding

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: How to save to a new worksheet.

    Quote Originally Posted by j_Southern
    The important difference between Excel2008 and 365 as far as this problem is concerned is that 2008 starts a new workbook with 3 sheets but 365 starts a new workbook with1 sheet (I think)
    As far as I know in all versions you can set the no of sheets to have in a new workbook via File>Options...
    If posting code please use code tags, see here.

  8. #8
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: How to save to a new worksheet.

    AliGW,
    Yes I can now see this the same thread. However I was so pleased with Arkadi's help that I marked the thread as solved. Too early it turned out. I needed more help so I started a new thread as the previous one was closed.
    I hope you can forgive me.
    John
    Norie,
    In gathering the information you needed I realised that I was looking at a clue as to what might be going on.
    I started the app from scratch and fired off the code above. I got an error message "subscript out of range" at the line With Sheets("TapeBudgetA").
    I repeated this after I had remed out all the code that was involved with Sheet("TapeBudgetA") and got a "subscript out of range" at the line SheetName="TotalBudgetA!...……….With Sheets(SheetName)
    It was then that I realised that I was looking at the code overlying a workbook called Book1 which had one sheet named "LabelBudgetA". I then decided to add the line "With Workbooks("NewBudgetv5.0.xlsm")" before the sheet names. When running this I got the same error message.
    I am now sure that my code is muddling up which workbook is active but I don't know how to sort this out.
    The important difference between Excel2008 and 365 as far as this problem is concerned is that 2008 starts a new workbook with 3 sheets but 365 starts a new workbook with1 sheet (I think)
    John
    P.S. I have been chastised for starting a new thread to cover the same subject. This posting was on the illegal thread, so I hope you see it here.
    I have just seen Norries comment which may mean the old code could be used, but I would like to see a more general answer if possible

+ 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. Save worksheet as workbook but with 'save as' feature
    By jjin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-14-2016, 04:43 PM
  2. Macro to save worksheet doesnt save correctly
    By 1967chris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2015, 10:19 PM
  3. [SOLVED] save open worksheet to file and include date of save
    By shaz0503 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-12-2014, 08:50 AM
  4. 'Automatically' Save a WorkSHEET CSV file on SAVE
    By jacalakie in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2013, 08:00 PM
  5. Macros To Save Worksheet as New xlsx file & Save Another Worksheet As A Text File
    By KeithT in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-16-2011, 05:13 PM
  6. Replies: 3
    Last Post: 06-27-2005, 01:05 AM
  7. Worksheet Buttons (Save, Save As, Cut, Paste, etc.) Not Working
    By SuzieQ12345 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-21-2005, 11:06 AM

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