+ Reply to Thread
Results 1 to 6 of 6

Worksheets.Add does not work with ( )

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-09-2013
    Location
    City, World
    MS-Off Ver
    Office 365
    Posts
    248

    Worksheets.Add does not work with ( )

    Hi

    Why when I use ( ) for the Add method, I got error message? It only works without ( ). I am just trying to experiment how and when to use ( ) and when not. Thank you.

     Worksheets.Add (before:=Worksheets(x))
    Sub my_add_sheet3()
        
        Dim x As Integer
        x = InputBox("Add before which sheet? Enter sheet number")
        Worksheets.Add before:=Worksheets(x)
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Worksheets.Add does not work with ( )

    Hi lastnn30,

    What error message do you get? If (as I suspect) it's "Run-time error 9" it's either because you have not assigned any positive value to x or the number is more than the actual sheets in the workbook.

    IMO I would not use the extra brackets as you have done in your first macro as they are not needed and it makes it easier to review without them.

    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    04-09-2013
    Location
    City, World
    MS-Off Ver
    Office 365
    Posts
    248

    Re: Worksheets.Add does not work with ( )

    Thank you. The message is "add ="

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Worksheets.Add does not work with ( )

    Thank you. The message is "add ="
    Yes it seems you have to remove the extra brackets (parentheses).

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,320

    Re: Worksheets.Add does not work with ( )

    You only use parentheses if:
    1. You are using Call; or
    2. You are storing the result of a function/method; or
    3. You are explicitly trying to evaluate/dereference an argument.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    2,082

    Re: Worksheets.Add does not work with ( )

    For example, if you wanted to assign the added worksheet to a variable, you'd add the brackets.

    Sub my_add_sheet3()
        
        Dim x As Integer
        x = InputBox("Add before which sheet? Enter sheet number")
        Set NewWorksheet = Worksheets.Add(before:=Worksheets(x))
        
    End Sub

+ 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. Form that will work off of 2 worksheets
    By readysetgo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-12-2021, 12:28 PM
  2. How to use macros that can work on different Worksheets
    By SteveBA in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2017, 03:00 AM
  3. [SOLVED] need code to work on all worksheets
    By prdponce in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-14-2017, 10:09 AM
  4. [SOLVED] Macros to work in all worksheets
    By bigband1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-16-2016, 09:58 AM
  5. [SOLVED] Worksheets Sort does not work
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-17-2014, 05:00 AM
  6. I would like to work across two worksheets and use a drop down box.
    By Levi427 in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 2
    Last Post: 05-26-2013, 11:22 PM
  7. Work with Multiple worksheets
    By tsec in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-08-2005, 12:51 PM

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