+ Reply to Thread
Results 1 to 10 of 10

Wildcards in VBA

  1. #1
    Registered User
    Join Date
    06-16-2006
    Posts
    5

    Wildcards in VBA

    I'm trying to use a formula that will check the contents of a cell and color the cell's row accordingly. However, the "keywords" that I'm looking for won't be the only thing in the cell - they will most likely be part of a sentence.
    With regular formulas, I found that just putting asterisks around the keywork insde quotation marks works fine. In VBA, though, that doesn't seem to work.
    Any ideas?

  2. #2
    Andrew Taylor
    Guest

    Re: Wildcards in VBA

    You can use the Like keyword for wildcard comparisons:

    If strCellValue Like "*" & strTest & "*" Then...

    Be aware that it's case-sensitive, so "A" is not Like "a". If
    you need case-insentivity use something like

    If LCase(strCellValue) Like "*" & LCase(strTest) & "*" Then...

    Andrew


    oodam wrote:
    > I'm trying to use a formula that will check the contents of a cell and
    > color the cell's row accordingly. However, the "keywords" that I'm
    > looking for won't be the only thing in the cell - they will most likely
    > be part of a sentence.
    > With regular formulas, I found that just putting asterisks around the
    > keywork insde quotation marks works fine. In VBA, though, that doesn't
    > seem to work.
    > Any ideas?
    >
    >
    > --
    > oodam
    > ------------------------------------------------------------------------
    > oodam's Profile: http://www.excelforum.com/member.php...o&userid=35513
    > View this thread: http://www.excelforum.com/showthread...hreadid=553317



  3. #3
    John
    Guest

    Re: Wildcards in VBA

    Would the InStr function work for you?

    Best regards

    John

    "oodam" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I'm trying to use a formula that will check the contents of a cell and
    > color the cell's row accordingly. However, the "keywords" that I'm
    > looking for won't be the only thing in the cell - they will most likely
    > be part of a sentence.
    > With regular formulas, I found that just putting asterisks around the
    > keywork insde quotation marks works fine. In VBA, though, that doesn't
    > seem to work.
    > Any ideas?
    >
    >
    > --
    > oodam
    > ------------------------------------------------------------------------
    > oodam's Profile:
    > http://www.excelforum.com/member.php...o&userid=35513
    > View this thread: http://www.excelforum.com/showthread...hreadid=553317
    >




  4. #4
    Charlie
    Guest

    RE: Wildcards in VBA

    Try using Instr:

    If InStr(Cells(1, 1), MyStr) > 0 Then MsgBox "Found it"



    "oodam" wrote:

    >
    > I'm trying to use a formula that will check the contents of a cell and
    > color the cell's row accordingly. However, the "keywords" that I'm
    > looking for won't be the only thing in the cell - they will most likely
    > be part of a sentence.
    > With regular formulas, I found that just putting asterisks around the
    > keywork insde quotation marks works fine. In VBA, though, that doesn't
    > seem to work.
    > Any ideas?
    >
    >
    > --
    > oodam
    > ------------------------------------------------------------------------
    > oodam's Profile: http://www.excelforum.com/member.php...o&userid=35513
    > View this thread: http://www.excelforum.com/showthread...hreadid=553317
    >
    >


  5. #5
    Nigel
    Guest

    Re: Wildcards in VBA

    You could try using Instr that will return a value > 0 if the string is
    found. e.g.

    If Instr(string1, string2) > 0 then
    'found
    else
    'not found
    end if

    Where string2 is the string to search for in string1. It is case sensitive
    as shown
    --
    Cheers
    Nigel



    "oodam" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I'm trying to use a formula that will check the contents of a cell and
    > color the cell's row accordingly. However, the "keywords" that I'm
    > looking for won't be the only thing in the cell - they will most likely
    > be part of a sentence.
    > With regular formulas, I found that just putting asterisks around the
    > keywork insde quotation marks works fine. In VBA, though, that doesn't
    > seem to work.
    > Any ideas?
    >
    >
    > --
    > oodam
    > ------------------------------------------------------------------------
    > oodam's Profile:
    > http://www.excelforum.com/member.php...o&userid=35513
    > View this thread: http://www.excelforum.com/showthread...hreadid=553317
    >




  6. #6
    Jim Thomlinson
    Guest

    RE: Wildcards in VBA

    You probably want to use the InStr function to determine if your substring
    exists in the full string. If Instr returns a value then you have found your
    substring, otherwise not.

    --
    HTH...

    Jim Thomlinson


    "oodam" wrote:

    >
    > I'm trying to use a formula that will check the contents of a cell and
    > color the cell's row accordingly. However, the "keywords" that I'm
    > looking for won't be the only thing in the cell - they will most likely
    > be part of a sentence.
    > With regular formulas, I found that just putting asterisks around the
    > keywork insde quotation marks works fine. In VBA, though, that doesn't
    > seem to work.
    > Any ideas?
    >
    >
    > --
    > oodam
    > ------------------------------------------------------------------------
    > oodam's Profile: http://www.excelforum.com/member.php...o&userid=35513
    > View this thread: http://www.excelforum.com/showthread...hreadid=553317
    >
    >


  7. #7
    Registered User
    Join Date
    06-16-2006
    Posts
    5
    Thanks everyone for your help. Much to my surprise, it actually seems to be working!

  8. #8
    Registered User
    Join Date
    11-13-2009
    Location
    Cumbria
    MS-Off Ver
    Excel 2016
    Posts
    87

    Re: Wildcards in VBA

    I've created a VBA tool for searching for cells that have certain text appearing somewhere by entering the text into a UserForm textbox and pressing search. It works, but the problem is that I might want every cell where the name Joe Bloggs or J. Bloggs appears, but not Mark Bloggs. Is there a way that I can modify the tool so that the user can enter, for example J* Bloggs in the textbox to get the contents of every cell with Joe Bloggs or J. Bloggs?

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Wildcards in VBA

    RowanB,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  10. #10
    Registered User
    Join Date
    11-13-2009
    Location
    Cumbria
    MS-Off Ver
    Excel 2016
    Posts
    87

    Re: Wildcards in VBA

    Thanks a lot Arlu. Sorry, I was thinking that the rule was to not create a new thread to avoid clutter.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1