Consulting

Results 1 to 10 of 10

Thread: Cycle through sheets

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Cycle through sheets

    I want to cycle through all the sheets in an openworkbook to see if there is any with a certain condition. Here is the code I tried. Where am I going wrong?

    [vba]Sub test()
    Dim detailtest As Worksheet
    Set ActiveWorkbook = wB(1)
    For detailtest = Worksheet(1) To Worksheet(2)
    On Error Resume Next
    Set findString = Range("C1", "D1").Find(What:="Whse")
    If findString Is Nothing Then

    Else
    MsgBox "it works."
    Next detailtest
    End Sub
    [/vba]

  2. #2
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I have it closer now. Here is my new code:

    [vba]
    For Each detailtest In wB(1).Sheets
    On Error Resume Next
    Set findString = Range("C1", "D1").Find(What:="Whse")
    If findString Is Nothing Then
    MsgBox "not this one"
    Else
    Set detail = wB(1).ActiveSheet

    MsgBox "it works."
    End If
    Next
    [/vba]

    If the sheet meets the condition I need it to activate the sheet

  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Try... [vba]
    Option Explicit

    Sub test2()
    'for two sheets
    Dim N As Long, Findstring As Range

    For N = 1 To 2
    Set Findstring = Worksheets(N).Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then MsgBox "it works."
    Next

    End Sub

    Sub TestAll()
    'for all sheets
    Dim detailtest As Worksheet, Findstring As Range

    For Each detailtest In Worksheets
    Set Findstring = detailtest.Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then MsgBox "it works."
    Next

    End Sub
    [/vba]EDIT: In the 1st procedure I took your use of 'Worksheet(1) To Worksheet(2)' to mean you only wanted to look at two worksheets. I you want to look at every worksheet use
    For N = 1 To Worksheets.Count instead of For N = 1 To 2
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    John thank you mine is working where with the msgbox but how would I select the sheet that meets the condition and then end the loop

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    E.G.[VBA]
    Sub TestAll()

    'for all sheets
    Dim detailtest As Worksheet, Findstring As Range

    For Each detailtest In Worksheets
    Set Findstring = detailtest.Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then
    detailtest.Activate
    Findstring.Select
    Exit For
    End If
    Next

    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    It is only working if I have the tab with Whse in it activated. Not if another one of the tabs are selected.

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I stepped through the code and it isn't going to the next sheet

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    ??? not having that problem on my machine??? Try this variation... [VBA]
    Sub TestAll()
    'for all sheets
    Dim detailtest As Worksheet, Findstring As Range

    For Each detailtest In Worksheets
    detailtest.Activate
    Set Findstring = detailtest.Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then
    Findstring.Select
    Exit For
    End If
    Next

    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    This is what I have and it is not working:

    [VBA]For Each detailTest In Worksheets
    On Error Resume Next
    Set Findstring = Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then
    Findstring.Select
    Set detail = wB(1).ActiveSheet
    MsgBox "it works."
    Exit For
    End If
    Next[/VBA]

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by Djblois
    This is what I have and it is not working:

    [vba]For Each detailTest In Worksheets
    On Error Resume Next
    Set Findstring = Range("C1", "D1").Find(What:="Whse")
    If Not Findstring Is Nothing Then
    Findstring.Select
    Set detail = wB(1).ActiveSheet
    MsgBox "it works."
    Exit For
    End If
    Next[/vba]
    This line... [VBA]Set Findstring = Range("C1", "D1").Find(What:="Whse")[/VBA]because you have not specified a worksheet the default is 'ActiveSheet', and hence only the active sheet is being searched. You must specify, use [VBA]Set Findstring = detailtest.Range("C1", "D1").Find(What:="Whse")[/VBA]and what does this mean? [VBA]Set detail = wB(1).ActiveSheet[/VBA]where are detail and wB declared?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

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