+ Reply to Thread
Results 1 to 13 of 13

Insert Desired count of new worksheets

Hybrid View

  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,054

    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

    Sub CreateSheets()
    Dim Count
    
        On Error Resume Next
    
            Application.DisplayAlerts = False
    
            
    
                Count = Application.InputBox _
                 (Prompt:="How many sheets do you want to insert?", _
                 Title:="HOW MANY?", Type:=1 + 8)
    
                 
    
        On Error GoTo 0
    
            Application.DisplayAlerts = True
    
    
    
        If IsNumeric(Count) And Count <> 0 Then
    
            Worksheets.Add After:=Worksheets(Worksheets.Count)
    
        Else
    
           Exit Sub
    
        End If
    
    End Sub
    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,976

    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.

    Sub CreateSheets()
    Dim Count
    Dim lngIndex As Long
    
        On Error Resume Next
    
            Application.DisplayAlerts = False
    
            
    
                Count = Application.InputBox _
                 (Prompt:="How many sheets do you want to insert?", _
                 Title:="HOW MANY?", Type:=1 + 8)
    
                 
    
        On Error GoTo 0
    
            Application.DisplayAlerts = True
    
    
    
        If IsNumeric(Count) And Count <> 0 Then
            For lngIndex = 1 To Count
                Worksheets.Add After:=Worksheets(Worksheets.Count)
            Next
        Else
    
           Exit Sub
    
        End If
    
    End Sub
    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
    Option Explicit
    
    Sub CreateSheets()
    Dim iCount
    Dim i As Integer
      On Error Resume Next
            Application.DisplayAlerts = False
    
                iCount = Application.InputBox _
                 (Prompt:="How many sheets do you want to insert?", _
                 Title:="HOW MANY?", Type:=1 + 8)
    
      On Error GoTo 0
    
            Application.DisplayAlerts = True
        If IsNumeric(iCount) And iCount <> 0 Then
     For i = 1 To iCount
          Worksheets.Add After:=Worksheets(Worksheets.Count)
          Next i
        Else: Exit Sub
        End If
    End Sub
    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

    Sub CreateSheets()
    Dim Count
    
        On Error Resume Next
    
            Application.DisplayAlerts = False
    
            
    
                Count = Application.InputBox _
                 (Prompt:="How many sheets do you want to insert?", _
                 Title:="HOW MANY?", Type:=1 + 8)
    
                 
    
        On Error GoTo 0
    
            Application.DisplayAlerts = True
    
    
    
        If IsNumeric(Count) And Count <> 0 Then
    
            Worksheets.Add Count:=Count, After:=Worksheets(Worksheets.Count)
    
        Else
    
           Exit Sub
    
        End If
    End Sub

  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,054

    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.
            Worksheets.Add After:=Worksheets(Worksheets.Count)
    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,054

    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,976

    Re: Insert Desired count of new worksheets

    Correct.

  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,054

    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