Consulting

Results 1 to 5 of 5

Thread: VBA Find question

  1. #1

    VBA Find question

    Hi, hope this is a quick question but I'm a VBA newbie so here goes.

    Trying to format certain cells in my worksheet base the value inside. However, not all of the values I'm looking for is in every worksheet I need to format. So for example, if I'm trying to format cells with the word "banana" but some sheets doesn't have "banana" then it kills the macro and stops looking for the next value.

    I thought that I could build an if statement around it but it's not working out too well. What I'm using is Cells.Find(What:="banana"....).Activate. Is this workable or do I need to do something totally different? Any help is greatly appreciated.

    M.Yu

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Do something like

    [vba]

    On Error Resume Next
    Set cell = Cells.Find(What:="banana"....)
    On Error Goto 0
    If Not cell Is Nothing

    'do stuff
    End If[/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
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    [vba]Sub bananaChecker()
    Dim sh As Worksheet
    Dim cell As Range
    For Each sh In ActiveWorkbook.Worksheets
    For Each cell In sh.UsedRange

    If cell.Text = "Bananas" Then
    cell.Font.Bold = True
    End If
    Next cell
    Next sh

    End Sub[/vba]
    ------------------------------------------------
    Happy Coding my friends

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by xld
    Do something like

    [vba]

    On Error Resume Next
    Set cell = Cells.Find(What:="banana"....)
    On Error Goto 0
    If Not cell Is Nothing

    'do stuff
    End If[/vba]
    May I ask why you have the error handler round the Find?
    Be as you wish to seem

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Aflatoon
    May I ask why you have the error handler round the Find?
    Howdy Aflatoon,

    Cuz the blue lettering looks pretty? Kidding of course and not to answer for Bob, but I do the same thing most of the time, out of habit from setting a ref to a (maybe exists, maybe not) sheet or wb.

    Mark

Posting Permissions

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