+ Reply to Thread
Results 1 to 9 of 9

Finding Exact Text via VBA

  1. #1
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    2007
    Posts
    123

    Finding Exact Text via VBA

    Hello,

    I am trying to have a warning for a macro that I'm running to alert the user if there isn't a match on Gender description. I am using a lookup cell within the workbook that sets a filter and pulls the applicable data to put into a report. If the macro doesn't find the lookup value, my workbook basically crashes (probably due to beginner coding). Any way, when the values are set correctly it works great some I'm doing with what I've got. Here's my issue, the following code is finding the word "Men" in Women and allowing the macro to continue. Is there a more specific function in vba that will take the "absolute" value of the lookup cell?

    Whe in set the filter in the workbook, I was able to switch "Search" for "Find" and it worked. With this being a "find" statement already, I'm a bit lost.

    Please Login or Register  to view this content.
    Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,445

    Re: Finding Exact Text via VBA

    Think you just need to change this
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,445

    Re: Finding Exact Text via VBA

    To quote someone else:
    If you do a manual Find, you'll see (under the options button) an option to
    "Match entire cell contents".

    The xlwhole is the same as checking this box. xlpart is like leaving it
    unchecked.

  4. #4
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    2007
    Posts
    123

    Re: Finding Exact Text via VBA

    Ah, this is great help. Thanks for pointing that out! I'll have to remember that for down the road.

    Unfortunately, this didn't work for this particular scenario. The cell I'm searching the gender in (B1:C1) will contain a full like of text.

    For example, it may say "Women 30-34" or "Men 1-15" so the cell contents don't match up entirely.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Finding Exact Text via VBA

    Can you give us a Search string and the cell value that you want to match to?

  6. #6
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    2007
    Posts
    123

    Re: Finding Exact Text via VBA

    B3 = Girls
    B4 = Men

    DataB1 = Women 30-34

    Therefore:


    FemaleStringToFind = Women
    MaleStringToFind = Men

    With Worksheets("Data").Range("B1:C1")

    Set FemaleRangeResult = .Find(What:=FemaleStringToFind, LookAt:=xlPart)
    Set MaleRangeResult = .Find(What:=MaleStringToFind, LookAt:=xlPart)

    If Not FemaleRangeResult Is Nothing Then <--------This should come up False since it cant find "Girls" in the string and search for MaleStringToFind
    GoTo GenderReportContinue

    ElseIf Not MaleRangeResult Is Nothing Then <----This is coming up as True because it finds "Men" in woMEN, but I need it to come up as false because it is not an exact match
    GoTo GenderReportContinue

    Else
    MsgBox "WARNING: GENDER NOT FOUND"
    GoTo WritePDF


    End If
    End With

    I hope that makes sense

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,524

    Re: Finding Exact Text via VBA

    1)
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    2)
    Try change to
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    12-09-2015
    Location
    Virginia, USA
    MS-Off Ver
    2007
    Posts
    123

    Re: Finding Exact Text via VBA

    Ahhhh...you guys put me on the right track with that XLWhole tip. I added a MatchCase function which did the trick!

    LookAt:=xlPart, MatchCase:=True

    Thank you all!

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,445

    Re: Finding Exact Text via VBA

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Find exact text in exact cell but on any sheet?
    By nobodyukno in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2017, 02:59 PM
  2. Finding two exact matches in a table
    By IanAramancm in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 05-22-2015, 01:47 PM
  3. Finding the cells that contain the same exact text as a specific cell
    By Cumbo in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 12-17-2014, 02:28 AM
  4. Finding not exact matches
    By anthonyrambo in forum Excel General
    Replies: 7
    Last Post: 07-08-2011, 03:37 PM
  5. finding exact word!
    By miyat in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-01-2009, 04:14 AM
  6. Finding an Exact Match
    By kent@romanoffco in forum Excel General
    Replies: 1
    Last Post: 10-02-2008, 10:59 PM

Tags for this Thread

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