+ Reply to Thread
Results 1 to 12 of 12

VB Number Matching Help Required

  1. #1
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    VB Number Matching Help Required

    Hi all,

    I have run into an odd error in a program I'm writing, and have isolated the problem with a smaller sub-code. Basically my code checks to see if the first digit in an integer matches a whole number in another cell. For the most part the code works fine, but in some situations it fails to recognize matches and I can't figure out why. The following is what my sheet looks like, as well as the code I wrote to check this error.

    Col.A Col.B
    R1 2.11 2
    R2 2.21 2
    R3 2.32 2
    R4 2.12 2
    R5 2.22 2
    R6 2.32 2

    Please Login or Register  to view this content.
    I don't know if there is a basic solution for this, but I'm stumped, and any help would be greatly appreciated.
    Last edited by hamhains; 07-27-2011 at 01:13 PM.

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    I *think* you need something like this....

    Please Login or Register  to view this content.
    Does that help?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: VB Number Matching Help Required

    Ron,

    Thanks for the reply. That didn't seem to help, it actually made the code return values of "no" for every instance now.

    Do you have any idea why the code I wrote is working for some digit combinations and not others? To me it seems arbitrary although it is probably a function of how excel is actually treating the variables.

  4. #4
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    Try running the macro in the attached workbook and let me know if I made any bad assumptions.

    I get these results:
    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: VB Number Matching Help Required

    Ron,

    I think I mis-stated my question. The code I had was meant to match the 2 before the decimal place. I'm trying to have my program recognize only the whole portion of each entry. In the workbook I posted above, ideally, every cell in column C would result in a "yes".

    I hope this helps

  6. #6
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    OK...thanks for the explanation.

    Try this:
    Please Login or Register  to view this content.
    Does that work for you?

  7. #7
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: VB Number Matching Help Required

    Thanks Ron,

    The code still doesn't work as it should. The output I get is "yes, yes, no, no, no, no". I'm not sure if this an artifact of excel numerical methods, as when I step through the code it literally reads IF (2) = (2), but treats it as false.

  8. #8
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    Perhaps if you post a sample workbook that demonstrates the issue, we'll be able to figure out the cause.

  9. #9
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: VB Number Matching Help Required

    Ron,

    Please see attached.
    Attached Files Attached Files

  10. #10
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    OK...I'm very puzzled.

    I run the macro I posted...and get incorrect results for rows 3 through 6.
    Yet...If I replace the 2's in B3:B6 with 2's (Yes, I typed the same number over them), the resulting values are correct!

    In fact, if I select B1:B6 then Data.Text-to-Columns...Finish
    the resulting values are also correct.

    I have no idea why or how that would happen. Did you type in those Col_B values or were they pasted in from another application?

  11. #11
    Registered User
    Join Date
    03-10-2011
    Location
    Guelph, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: VB Number Matching Help Required

    Hmm, that is weird. I did the same and it worked. I wrote the sheet on my own for comparison purposes, and did no copy or pasteing. No idea, but thanks!

  12. #12
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: VB Number Matching Help Required

    This code seems to work...even on the original data...
    Please Login or Register  to view this content.

+ 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