Consulting

Results 1 to 6 of 6

Thread: Solved: AutoFilter Question

  1. #1
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location

    Solved: AutoFilter Question

    Hi,

    I have the following code that does an autofilter in Column B and places the information into Column C but will give me an error message when the Criteria is blank. How can I bybass this error message?
    I get a Run-time error '1004': No Cells were found.


    [vba]iLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    Selection.AutoFilter Field:=2, Criteria1:="=*BROWN*", Operator:=xlAnd
    Range("C5:C" & iLastRow & " ").SpecialCells(xlCellTypeVisible).Value = "BROWN"[/vba]


    thanks
    zach

  2. #2
    VBAX Tutor
    Joined
    Oct 2007
    Posts
    210
    Location
    look up On Error in the help files for a detailed description
    "The amount of stupid people in the world is God's way of punishing the smart people" - protean_being

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

    iLastRow = Cells(Rows.Count, "B").End(xlUp).Row
    Selection.AutoFilter Field:=2, Criteria1:="=*BROWN*", Operator:=xlAnd
    On Error Resume Next
    Set rng = Range("C5:C" & iLastRow & " ").SpecialCells(xlCellTypeVisible)
    On Error Goto 0

    If Not rng Is Nothing Then rng.Value = "BROWN"
    [/vba]
    Last edited by Bob Phillips; 01-31-2008 at 01:17 AM.
    ____________________________________________
    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

  4. #4
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    EL XLD,

    thanks again for your solution.
    i added the following
    [vba]
    Set rng = Nothing
    [/vba]
    after
    [vba]On Error Resume Next[/vba]
    and that works


    ProteanBeing:

    thanks for the tip; sometimes i find the help files a little daunting

    zach

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I assume that that means you are doing it within a loop?
    ____________________________________________
    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

  6. #6
    VBAX Tutor
    Joined
    Feb 2006
    Posts
    295
    Location
    hi xld,

    i'm not doing this within a loop yet.
    if i leave out
    Set rng = Nothing
    i get the following error message:
    Run-time error '424':
    Object required

    wasn't sure how to fix this so i "read" some help files which i never seem to fully comprehend

    i will post a new question regarding how to loop through a list of criteria based on entries from a column in another sheet..

    thanks again
    zach

Posting Permissions

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