+ Reply to Thread
Results 1 to 7 of 7

Select Multiple Sheets In A Vb Code

Hybrid View

  1. #1
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Red face Select Multiple Sheets In A Vb Code

    I'm trying to select the first 8 sheets of a workbook with 30 sheets, so that I can perform a search operation in all those 8 sheets.
    Since the name of those 8 sheets could change, I tryed the following code:
    Dim MYARRAY(8) As String
    MYARRAY(1) = Sheets(1).Name
    MYARRAY(2) = Sheets(2).Name
    MYARRAY(3) = Sheets(3).Name
    MYARRAY(4) = Sheets(4).Name
    MYARRAY(5) = Sheets(5).Name
    MYARRAY(6) = Sheets(6).Name
    MYARRAY(7) = Sheets(7).Name
    MYARRAY(8) = Sheets(8).Name
    Sheets(MYARRAY).Select
    The problem is that when I get to the last statement (Sheets(myarray).Select), I get error 9, "Subscript out of range".
    Can somebody , please help? I'm going nuts!

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good evening Deci

    It doesn't haveto be quite so complicated. How about :

    Sheets(Array(1, 2, 3, 4, 5, 6, 7, 8)).Select
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    05-26-2007
    Posts
    53

    Smile

    Thank you! I can't belive how simple it was!

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Deci, try using the Macro Recorder, that would have given you the code.
    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 dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi royUK

    Using the macro recorder will indeed give you the code, however, it will give you the code using the sheets name. However, as the name may well change you would have to refer to them by number.

    Nevertheless using the macro recorder is a great tip that many users forget is available as their experience grows - most of us actually start to learn from the macro recorder.

    DominicB

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Domenic, I was only pointing out that the Recorder can often give you the basic code needed rather than waiting for a reply on a Forum. Agreed, the code generally needs working on.

+ 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