Consulting

Results 1 to 3 of 3

Thread: Switch to second worksheet or add new one

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

    Switch to second worksheet or add new one

    I am trying to write code to switch to the second worksheet if there is one and if there isn't a worksheet I would like to add a new sheet. Is this possible?

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Sub AddSheet()
    On Error GoTo NewSheet
    Sheets(2).Activate
    Exit Sub
    NewSheet:
    Sheets.Add
    End Sub
    [/VBA]
    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'

  3. #3
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Similar to Malcolms, just showing another way to do it:[vba]Sub SheetAdd2()
    If Sheets.Count = 1 Then Sheets.Add
    Sheets(2).Activate
    End Sub[/vba]Matt

Posting Permissions

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