Consulting

Results 1 to 14 of 14

Thread: Add page if not enough

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Add page if not enough

    I am using this code to add a worksheet if it needs to:

    [VBA]If WB.Sheets.Count = 1 Then
    Set Cust = WB.Worksheets.Add
    Else: Set Cust = WB.Sheets(2)[/VBA]

    However this works if it needs to add the sheet but not if it doesn't need to add the sheet? Please help.

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    If I get you correctly, your issue is that your new sheet is added before the exisitng sheet. Setting to Worksheets(2) then activates the second sheet in the workbook, which was originally your first sheet.

    Try this:
    [vba]Sub test()
    Dim wb As Workbook
    Dim cust As Worksheet

    Set wb = ActiveWorkbook
    If wb.Sheets.Count = 1 Then
    Set cust = wb.Worksheets.Add(, wb.Sheets(1))
    Else
    Set cust = wb.Worksheets(2)
    cust.Activate
    End If
    End Sub[/vba]

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    That code is also only working if there is only one tab already but if there are 2 tabs already it doesn't work. I don't care where it adds it, I only care that it does add it and then switches to the new tab.
    Last edited by Djblois; 07-14-2006 at 06:20 AM.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You are not clear. It works perfectly for me.

    DO you mean you want to add a new sheet regardless, after the last sheet? If so, use

    [vba]

    Set Cust = WB.Worksheets.Add(After:=Wb.Worksheets(WB.Worksheets.Count))
    [/vba]

  5. #5
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    No I only want to add a sheet if there are not enough sheets.

  6. #6
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    what's "enough"?

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    It shows im my code at the top if there are less than 2 then I want it to add a sheet. THe code is working if it needs to add it but it isn't working if it doesn't need to add a sheet.
    Last edited by Djblois; 07-14-2006 at 07:43 AM.

  8. #8
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    good point. sorry, i had a bit of a mental lapse (due to laziness to fully read any of your code)

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by Djblois
    It shows im my code at the top if there are less than 2 then I want it to add a sheet. THe code is working if it needs to add it but it isn't working if it doesn't need to add a sheet.
    I'm sorry. Call me dense, if you like, but I really don't understand what you're asking.

    The code I provided will only add a sheet to the workbook if there is 1 worksheet (less than 2) in the book. If there are more sheets, then it sets the 2nd worksheet to a variable and activates that sheet. To test it, add a second sheet, select the first sheet in the workbook, then run the code. It will select sheet 2.

    Exactly what are you/are you not expecting to happen if there is more than 1 sheet? My guess is that it is just not doing what you really want it to do... As both Bob and I have pointed out, the code is doing what I told it to...

    Can you explain a little more?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Ken Puls
    I'm sorry. Call me dense, if you like, but I really don't understand what you're asking.

    The code I provided will only add a sheet to the workbook if there is 1 worksheet (less than 2) in the book. If there are more sheets, then it sets the 2nd worksheet to a variable and activates that sheet. To test it, add a second sheet, select the first sheet in the workbook, then run the code. It will select sheet 2.

    Exactly what are you/are you not expecting to happen if there is more than 1 sheet? My guess is that it is just not doing what you really want it to do... As both Bob and I have pointed out, the code is doing what I told it to...

    Can you explain a little more?
    What a thicko you are Ken!

    Oops, so am I because I don't understand either.

  11. #11
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    For some reason the code works if it needs to add a sheet. However, if it doesn't need to add a sheet, it doesn't switch to it.

    Daniel

  12. #12
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Daniel, please be more specific......what code doesn't work and doesn't switch to what.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  13. #13
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Thank you lucas I think it is working now

  14. #14
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    What a thicko you are Ken!

    Oops, so am I because I don't understand either.
    Hmmm... seems solved and I still feel thick!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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