Consulting

Results 1 to 6 of 6

Thread: Solved: Select Worksheets based on criteria

  1. #1
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location

    Solved: Select Worksheets based on criteria

    Hi all

    Have been visiting the site for a little while now and the day has come that I needs some help.

    I would like a routine/proceedure that will loop through all the sheets in a workbook and select all those that meet a certain criteria.

    One example I would like to do is select all sheets with an '@' in its sheet name.

    Any help much appriciated.

    Cheers
    Jay

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi Jay,

    Glad we could finally be of assistance for you To select multiple sheets you have to put their names into an array, I hope the following example explains everything for you:[vba]Sub NewDaddySelectSheets()
    Dim sArr() As String, Cnt As Long, WS As Object
    ReDim sArr(0)
    Cnt = 0
    For Each WS In ActiveWorkbook.Sheets
    If InStr(WS.Name, "@") Then
    ReDim Preserve sArr(Cnt)
    sArr(Cnt) = WS.Name
    Cnt = Cnt + 1
    End If
    Next
    If Cnt > 0 Then Sheets(sArr).Select
    Set WS = Nothing
    End Sub[/vba]If you have any questions, please ask!
    Matt

  3. #3
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location
    Hi Matt

    Thank you.
    I new I had to build an array and sort of had an idea for doing so but could not workout how to select them.

    I am just about to give it a run on a file I am working on now.
    I'll let you know how I get on.

    Cheers
    Jay

  4. #4
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location
    Quote Originally Posted by mvidas
    Hi Jay,

    Glad we could finally be of assistance for you To select multiple sheets you have to put their names into an array, I hope the following example explains everything for you:[vba]Sub NewDaddySelectSheets()
    Dim sArr() As String, Cnt As Long, WS As Object
    ReDim sArr(0)
    Cnt = 0
    For Each WS In ActiveWorkbook.Sheets
    If InStr(WS.Name, "@") Then
    ReDim Preserve sArr(Cnt)
    sArr(Cnt) = WS.Name
    Cnt = Cnt + 1
    End If
    Next
    If Cnt > 0 Then Sheets(sArr).Select
    Set WS = Nothing
    End Sub[/vba]If you have any questions, please ask!
    Matt
    Hi Matt

    Thanks for your help. I have managed to use this in a few scenarios already!

    Have another one that I need some help with.
    Using the above I would like ‘toggle’ hiding/showing the worksheets based in the array.
    I have some that I use to do this but it refers specifically to a worksheet(s) and I am not sure how to ‘assign?’ the various worksheets in the array to the visible true/false?

    I am looping through all worksheets in the active workbook and building an array of worksheet names that are like “TP Grid*”.

    And the vba I use and not sure how to adapt is as follows;

    On Error Resume Next
    If Application.Worksheets("Aptos").Visible Then
    Application.Worksheets("Aptos").Visible = False
    ElseIf Application.Worksheets("Aptos").Visible = False Then
    Application.Worksheets("Aptos").Visible = True
    End If

    Not sure if I am approaching this in the right way but your help would be really appreciated.

    Cheers
    Jay


  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For i = LBound(sArr) To UBound(sArr)
    Worksheets(sArr(i)).Visible = Not Worksheets(sArr(i)).Visible
    Next i
    [/vba]

  6. #6
    VBAX Regular
    Joined
    Oct 2006
    Location
    Wiltshire, UK
    Posts
    43
    Location
    Hi xld

    Thank you very much.
    It works like a dream.

    Sorry for taking a while but I did'nt know you responded.

    Cheers
    Jay

Posting Permissions

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