+ Reply to Thread
Results 1 to 13 of 13

Insert Desired count of new worksheets

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Insert Desired count of new worksheets

    I've got a macro that i think should be working, but it's not working the way I intend. What I want it to do is ask how many sheets need to be inserted, then take the input and insert that many sheets.

    It asks the question, but it only inserts one sheet, and I don't understand why. I've tried DIMming "Count" as Long, as a Range, and as an Integer, with no effect other than an error on "Range". It works the same just doing "DIM Count". I'd appreciate some help on this one.

    Thanks,
    John

    Please Login or Register  to view this content.
    Last edited by jomili; 01-10-2011 at 11:39 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Insert Desired count of new worksheets

    You are only issuing 1 worksheets.add command, hence you only get one sheet.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Insert Desired count of new worksheets

    You are not using the information in the Count variable to output more than 1 sheet.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Insert Desired count of new worksheets

    Try this
    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Insert Desired count of new worksheets

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Insert Desired count of new worksheets

    Wow, way much help! Thanks.

    All three macros posted seem to work magnificently, but I still don't understand why mine didn't.

    Andy and Roy,
    Would you explain why the "For lngIndex = 1 To Count", or the "For i = 1 To iCount"
    is necessary? I can see that it is, just not why.

    Watersev,
    You went a different route by putting "Count:=Count," in the middle of the ADD line. Can you explain why this was necessary, why Excel wouldn't have known that Count equaled Count?

    Thanks,
    John

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Insert Desired count of new worksheets

    As everybody has pointed out your code did not use the Count variable.

    I think you are confused about what the use of Count in this line is doing.
    Which is to position the newly added worksheet after the last worksheet.
    Please Login or Register  to view this content.
    Roy and I used a loop to add the correct number of sheets. But as watersev's code shows a loop is not required as there is a parameter to define the number of sheets to add.

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Insert Desired count of new worksheets

    You are correct that I am confused; it's my perpetual state of being.

    However, going to the line you pointed out, I would think the "Add After:=" would take care of telling how to position the sheet, so the ":=Worksheets(Worksheets.Count)" should do something, which at face value to me looks like it's defining how many of the Worksheets to add. But you're saying that's not the case with this bit of code?

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: Insert Desired count of new worksheets

    The After parameter does do something. It tells the method after which sheet then newly added one should be placed.

    The .worksheets(.worksheets.count) part identifies that sheet.

    .worksheets.count will return the index number of worksheets currently in the workbook. That value is then used to index the worksheets collection.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Insert Desired count of new worksheets

    In case it's clearer, imagine specifically that you already have 5 sheets - then the code is:
    Please Login or Register  to view this content.
    which is:
    Please Login or Register  to view this content.
    i.e. add the sheet after the 5th sheet.

  11. #11
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Insert Desired count of new worksheets

    Okay, let's see if I've got it:

    "After" tells the macro the methodology to use to place the worksheet.

    The .worksheets(.worksheets.count) identifies the sheet after which to place the worksheet (by counting the worksheets, identifying all existing as the current worksheet collection, then looking at the last one and after?)

    So then, Count in that line wasn't referring to my input at all, though my input variable was named Count. If I'd called my variable "Spot", the macro could have been written like this:

    Please Login or Register  to view this content.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,978

    Re: Insert Desired count of new worksheets

    Correct.

  13. #13
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,056

    Re: Insert Desired count of new worksheets

    If you could see me now you'd see a big lightbulb over my head! I think I've got it now.

    Thank you so much for patience and skill at explaining, and please accept my apologies for being particularly obtuse on this issue.

    Thanks,
    John

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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