Consulting

Results 1 to 6 of 6

Thread: Solved: error when looping

  1. #1
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location

    Solved: error when looping

    Wasn’t sure whether to start a new thread or not. But because the other one is now resolved, I thought I would.

    This is erroring out on

    [VBA]
    oSh.Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select
    [/VBA]

    [VBA]
    Sub Empty_Sheets()

    Dim oSh As Worksheet


    For Each oSh In ActiveWorkbook.Worksheets


    Select Case oSh.Name
    Case "a", "b", "c"

    oSh.Cells.EntireRow.Hidden = False
    oSh.Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select

    oSh.Selection.EntireColumn.Select
    oSh.Range(Selection, Selection.End(xlToLeft)).Select
    oSh.Selection.ClearContents
    oSh.Range("A1").Select

    Debug.Print oSh.Name

    End Select

    Next oSh

    End Sub
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Presumably, it is erroring because there is nothing found.

    You should not select, but set a range object to the find range, and test it for nothing, which means not found.
    ____________________________________________
    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
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    Total is there on all sheets. When I select the sheet it works, just not automatically

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    POst a workbbok to try it on please.
    ____________________________________________
    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

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    It would seem to me that the .Find would be dependent on where the ActiveCell is on any given sheet.

    What happens if you just delete the After:=ActiveCell ?

    I'd still follow XLD's suggetion to test for a not found condition also


    [VBA]
    oSh.Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select
    [/VBA]

    Paul

  6. #6
    VBAX Contributor
    Joined
    Apr 2008
    Posts
    136
    Location
    I did this instead and it worked.

    [vba]
    Sub macrotest ()
    Sheets(Array("a", "b", "c")).Select
    Cells.Find(What:="Total", After:=ActiveCell, LookIn:=xlFormulas _
    , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Select


    Selection.EntireColumn.Select
    Range(Selection, Selection.End(xlToLeft)).Select
    ' ActiveWorkbook.Names.Add Name:="TRange", RefersToR1C1:=Selection
    Selection.Clear
    'Cells.Selection.ClearContents
    Range("A1").Select

    End Sub
    [/vba]

Posting Permissions

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