Consulting

Results 1 to 4 of 4

Thread: Solved: How to selct sheets starting with certain word

  1. #1

    Solved: How to selct sheets starting with certain word

    Hi

    I have a simple problem. I have eight sheets, after running the programme they are all selected. I have to do following

    1. Deselct all sheets
    2. Select four sheets starting with word ABCD
    3. Deselect these sheets
    4. Select sheets starting with XYZW

    Thanks

    VIcks

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

    Dim aryNames As Variant
    Dim sh As Worksheet
    Dim i As Long

    With ActiveWorkbook

    ReDim aryNames(1 To .Worksheets.Count)

    For Each sh In .Worksheets

    If sh.Name Like "ABC*" Then

    i = i + 1
    aryNames(i) = sh.Name
    End If
    Next sh
    If i > 0 Then

    ReDim Preserve aryNames(1 To i)
    Worksheets(aryNames).Select
    End If

    i = 0
    For Each sh In .Worksheets

    If sh.Name Like "XYZ*" Then

    i = i + 1
    aryNames(i) = sh.Name
    End If
    Next sh
    If i > 0 Then

    ReDim Preserve aryNames(1 To i)
    Worksheets(aryNames).Select
    End If
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks xld

    Just wanted to check why cant the following formats work?
    [vba]
    ThisWorkbook.Worksheets(array("ABCD*")).Select[/vba]

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Because it doesn't.

    Worksheets expects a name or an array of names for selection, and array("ABCD*") is not an array of all names beginning with ABCD, it is just a single item array of ABCD*.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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