+ Reply to Thread
Results 1 to 5 of 5

VBA Select All But Four Sheets In Workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Select All But Four Sheets In Workbook

    Hi, I wonder whether someone may be able to help me please.

    From a number of threads I've found, I've put together the following script to select all sheets within my workbook, except for four.


    Sub SelectSheets()
    Dim sh As Worksheet
        
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> "Macros" And sh.Name <> "All Data" And sh.Name <> "Flexible Resources List" And sh.Name <> "Unique Records" Then
            sh.Select
        End If
    Next sh
    End Sub
    The script runs, but only selects the last, out of about twenty of the sheets, and I'm a little unsure why.

    I just wondered whether someone may be able to look at this please and let me know where I've gone wrong.

    Many thanks and kind regards

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA Select All But Four Sheets In Workbook

    Hi, hobbiton73,

    any Select you perform will only have one sheet to refer to, the last sheet you found. Maybe try it like this building an array and using that for selecting (although any Selection should be useless as action may be done withour this):
    Sub SelectSheets()
    Dim sh As Worksheet
    Dim strArr()
    Dim lngArray As Long
    
    lngArray = 1
    For Each sh In ActiveWorkbook.Worksheets
      Select Case sh.Name
        Case "Macros", "All Data", "Flexible Resources List", "Unique Records"
        Case Else
          ReDim Preserve strArr(1 To lngArray)
          strArr(lngArray) = sh.Name
          lngArray = lngArray + 1
      End Select
    Next sh
    Sheets(strArr).Select
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Select All But Four Sheets In Workbook

    Hi @HaHoBe, thank you very much for taking the time to reply to my post and for the solution, which perfectly, and I was particularly interested about your comment on how the 'Select' function works.

    Because I'm relatively new to VB, may I ask if you could possibly insert some comments into the code, so I can learn from this.

    Many thanks and kind regards

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA Select All But Four Sheets In Workbook

    Hi, hobbiton73,

    I´m a bit out of time by now - so it´s sort of "Typos To Go":
    Sub SelectSheets()
    Dim sh As Worksheet     'variable for worksheets
    Dim strArr()            'array to be used to hold the sheet names
    Dim lngArray As Long    'variable used as upper limit for teh array
    
    lngArray = 1            'starting value for the array
    For Each sh In ActiveWorkbook.Worksheets
      'check the worksheet name
      Select Case sh.Name
        Case "Macros", "All Data", "Flexible Resources List", "Unique Records"
          'these are the names not to select
        Case Else
          'code for all other sheets to work with
          'preserve means hold the values inside the array, redim means give a new upper bound for the array
          'we set the upper bound starting from 1 using our variable
          ReDim Preserve strArr(1 To lngArray)
          'put the name of the worksheet into the array as the last value in the array / upper bound
          strArr(lngArray) = sh.Name
          'add 1 to the variable for a possible next sheet to add
          lngArray = lngArray + 1
      End Select
    Next sh
    'we're through, just select all worksheets using the array we hold
    'you may run into an error if you only have sheets in the workbook that include names of sheets not to select
    Sheets(strArr).Select
    End Sub
    Ciao,
    Holger

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Select All But Four Sheets In Workbook

    Hi @HaHoBe, that's absolutely no problem at all, thank you very much for doing this.

    Kind Regards and all the best.

+ 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. Select Case within For loop not copying sheets to a new workbook
    By buttonmasher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-07-2013, 02:30 PM
  2. select external workbook and them merge all sheets in one
    By jmaocubo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-22-2009, 09:49 AM
  3. Re: Select All Sheets in Workbook
    By bttreadwell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-21-2005, 05:25 PM
  4. Select the same cell in different sheets in the same workbook?
    By PEA in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-03-2005, 03:05 AM
  5. Using Addin to select sheets in Active Workbook
    By ackurv in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2005, 04:28 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