+ Reply to Thread
Results 1 to 13 of 13

Two apparently identical strings not registering as equal in Excel VBA

  1. #1
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Two apparently identical strings not registering as equal in Excel VBA

    Hi everybody.

    My first post, so be nice to me!

    I'm struggling with a problem with some VBA I've written. I have a list of data in a Word table column, and a similar list of data in an Excel spreadsheet row. I'm trying to cycle through the two sets of data, and do various tasks when a match is found. My Excel VBA code looks like this:

    aString = objDoc.Tables(1).Cell(aCount, 2)
    bString = Sheets("Stuff").Cells(2, bCount).Value

    If aString = bString Then


    ...etc

    The problem is that it never returns any matches. I've put message boxes in to check, and even when the two strings look the same, it isn't being picked up by the If...Then statement. Everything else seems to be working fine - Excel is controlling Word properly and the data is being pulled out correctly etc. I've seen similar threads on this forum where people have had issues with spaces - I'm pretty sure that's not the case here. I've tried using 'Trim' on each string, and I've also checked in cases where there are only single words in the data - same problem.


    Am I missing something? Very grateful for any help. Thanks

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Yeah, they don't match.

    Have you tried opening the Locals Window and stepping through the code to see what the variables ARE when you think they should match?
    http://www.cpearson.com/excel/DebuggingVBA.aspx

    I found something odd like this yesterday and it turns out it was applying the Sheet name TO the string which I did not expect. I found it when stepping through the code with the Locals window and quickly changed the code to work the way I wanted it to.
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Hi, thanks for the quick reply.

    Yes, I've stepped through it and had a careful look. I've just double-checked - aString and bString are identical words, same case, same word, same everything, but yet the If/Then statement is not seeing it that way. Nothing inside the If/Then loop is being executed - I've watched it in the debugger, and I've put simple message boxes in there to shout out if it does detect a match. Really odd.
    Last edited by Big_Benjamin; 11-02-2016 at 04:26 PM. Reason: typo

  4. #4
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Two apparently identical strings not registering as equal in Excel VBA

    So it is not detecting a match.
    Maybe try TRIM() to see if you are missing a space at the end or something on one of them, creating a non-match?

  5. #5
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Hello again - already tried TRIM, same result.

  6. #6
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Use the immediate window. Do they match or not?
    You can also try to UCASE(string) both of them as VBA does not find that "MATCH"="Match"

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: Two apparently identical strings not registering as equal in Excel VBA

    As a debugging step, I would be tempted to put a few checks.

    Use the Len() function to check that the alleged matches are the same length.
    As a debugging step, do a character by character check of the alleged matches. Something like:
    Please Login or Register  to view this content.
    When the code stops, I can look at i and where I am at in the string and see if that tells me what is going on.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Here is a ditty I wrote a while back - we had a dashboard from which we imported data and it was fond of using ASCII 160 instead of ASCII 32 for a space.

    You can use this as fodder for a msgbox or run it as a UDF on the spreadsheet itself.

    It parses the string and puts out a string with the ASCII number equivalent for each character.
    Please Login or Register  to view this content.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  9. #9
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Hi - yep, they are perfect matches. I've just gone in to the data and forced two of the cells to just be the letter 'a', to keep it really simple. Same result. Most perplexing...

  10. #10
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    dflak - thanks for the reply. That's a really handy bit of code - I'll be keeping that. I don't think it's an ASCII issue, as I said in my reply to mikeTRON above, I have been in to the data and forced it to be the same, without success. That said, I'll give it a go, since I've exhausted everything else. The Word document is actually generated by another part of the Excel spreadsheet - I wondered if it might be something to do with the way that it transfers from Excel into Word. I'll let you know how I get on.

  11. #11
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Interesting...I used your ASCII tool and it showed a carriage return and 007 (Bell) character on every Word table cell, but not the Excel. Would that be enough to cause the issue?

  12. #12
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Probably. You could "scrub" the data by find and replacing CHAR(7) with "".

    P.S. Try CLEAN() first.
    Last edited by dflak; 11-03-2016 at 11:31 AM. Reason: Add PS

  13. #13
    Registered User
    Join Date
    11-02-2016
    Location
    Cornwall, England
    MS-Off Ver
    Mac - 2016
    Posts
    11

    Re: Two apparently identical strings not registering as equal in Excel VBA

    Thanks for all the replies. I ended up designing an alternative solution, where I compared the data that ended up in the Word document while it was still resident in Excel, and then found it in the Word table. This seemed to work well, and also avoided another issue that I had set myself up for. Really useful replies though - I never realised so many hidden ASCII characters were out there. Until the next time...

+ 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. (A - B) is not equal to A - B, apparently
    By edward_glyver in forum Excel General
    Replies: 3
    Last Post: 02-24-2016, 08:15 AM
  2. [SOLVED] Mark similar but not identical text strings
    By n_ant in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-05-2014, 06:00 PM
  3. Assining individual values to multiple identical strings
    By vivavilla in forum Excel General
    Replies: 1
    Last Post: 03-08-2011, 07:58 AM
  4. Seemingly identical text strings not matching
    By mr0range in forum Excel General
    Replies: 3
    Last Post: 08-27-2009, 05:28 PM
  5. Look for identical strings and add cell values beside each
    By GeorgY in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-19-2009, 02:03 AM
  6. Replies: 1
    Last Post: 08-04-2006, 03:20 PM
  7. Replies: 0
    Last Post: 09-14-2005, 07:05 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