+ Reply to Thread
Results 1 to 14 of 14

Lookup Long Reference Number?

  1. #1
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Lookup Long Reference Number?

    I'm having problems trying to lookup a long (18 digit) reference number. I'm working with credit card charges, where each charge has a unique 18-digit reference number. Data is processed each week. I am saving the reference numbers that have already been processed in a list (formatted as text). When we process new data, the input file contains all charges in the current billing cycle (which may include some that were already processed in the previous week), so we need to check each reference number to verify that it has not already been processed (i.e. is not in the list of processed numbers).

    I tried using:
    Please Login or Register  to view this content.
    where Reference is a STRING variable that is set to the reference number, but if the first 15 digits match, Excel will deem the values equal. For example, if 123456789012345000 is in the processed list, and a new charge with reference of 123456789012345123 is checked, the COUNTIF will return a 1, even though the looked-up value does not actually exist. Apparently Excel is "helpfully" converting the text values to numerical values before comparing them (and then looking only at the significant digits).

    I tried using MATCH instead of COUNTIF, but of course this returns an error if the reference value is not found. I tried using IFERROR with the MATCH worksheet function but that still just trips a runtime error!

    Is there any way to do this lookup (to simply check whether an 18-digit reference number exists in the processed list) without resorting to an error handler with the MATCH worksheet function? I feel like I'm missing some stupid-simple solution, but so far I haven't been able to come up with anything that works.

  2. #2
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Lookup Long Reference Number?

    Pl see enclosed file with macro and Excel function. B5 value is not counted as 1 in A1:A5 in macro and function.
    Pl upload file if more assistance is required.
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,410

    Re: Lookup Long Reference Number?

    Sorry for off-topic interjection:

    ONCE AGAIN, although there is no official rule regarding this behaviour, we request that wherever possible both the question AND the answer be provided in substantive detail here within the thread. An attached workbook is an excellent aid for posing a question and offering a solution, but solely doing that with no in thread explanation makes it difficult for researchers to understand or consider the Q & A of this thread without downloading what may be a pointless doc to them, if they can do that at all. Doing that also hides the content from search engines so others may never benefit from this.

    I'm sure you understand, and we look forward to seeing you post your formulas/macros in your posts for the searching benefit of all.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Lookup Long Reference Number?

    Ref Post#3
    @.AliGW
    I am sorry for missing details.
    Formula used is

    =COUNTIF(A1:A5,B5)

    Macro code is

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Lookup Long Reference Number?

    No, that solution does not work. You are not using 18-digit numbers.
    If you change 342111 to 3421112223334440000, and change B5 to 3421112223334440123, the COUNTIF function will find it as a match (countif = 1). That is my problem!

  6. #6
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Lookup Long Reference Number?

    Pl see file
    Formula
    =COUNTIF($C$1:$C$7,D5)
    Macro code
    Please Login or Register  to view this content.
    In both the cases result is 0.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Lookup Long Reference Number?

    Change cell D5 to 3421112223334440555 and COUNTIF then returns a value of 7!
    That is WRONG.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Lookup Long Reference Number?

    I think what happens here is that the COUNTIF() function is designed to receive a text string in the 2nd argument so that it can receive <, > type conditions. Internally, when the COUNTIF() function receives something that can be interpreted as a number, it converts it to a number (double precision) even when passed as text to the function.

    Have you tried the COUNTIFS() function to see if it behaves the same? I don't have Excel here, so I cannot test for you, but you might try that to see if COUNTIFS() is the same.

    You call this a lookup function, but I usually don't think of COUNTIF() as a lookup function. Have you tried an actual lookup function (like MATCH())? MATCH() is usually better at distinguishing between numbers and numbers as text. Of course, rather than testing for a count of 0 or not 0, you will be testing for "did MATCH() return a number or did it return an error". I know you have to be careful how you write the statement, because one way of calling the worksheet function will cause VBA to terminate anytime the function returns an error (preventing you from testing for the error condition afterwards) and another way of calling the worksheet function will store the error and allow you to test for the error afterwards. I'm not savvy enough to remember exactly how to do it (it has to do with whether or not you use the WorksheetFunction collection to call the function or not).

    Are you limited to Excel? I have LO Calc here, and LO Calc's COUNTIF() function can distinguish between 12345678901234567890 and 12345678901234568907, so it doesn't seem to have the same problem as Excel's implementation. Of course, where this is part of a macro, that could mean a lot of extra work trying to convert your macro into a language that another spreadsheet can understand.

    If push comes to shove, you can always write your own countif type function in VBA where you can control if, when, and how VBA converts a number as text to a number.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Lookup Long Reference Number?

    A quick internet search found this rundown on error handling when calling worksheet functions: https://rubberduckvba.wordpress.com/...on-and-errors/

  10. #10
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Lookup Long Reference Number?

    Yes, I tried using the MATCH worksheet function, but that trips a runtime error when the value is not found. I'd like to avoid using an error handler (or a user-defined function) if possible. I originally used COUNTIF because it will NOT trip a runtime error if no matches are found (it just returns a value of zero).
    I'm guessing maybe the simplest solution is to just save the processed reference numbers with an added character (such as R111122223333444455) to force everything to text, and then lookup the new reference number with an "R" prefixed.

  11. #11
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,811

    Re: Lookup Long Reference Number?

    Prepending a non-numeric character might be easiest.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Lookup Long Reference Number?

    Am I missing something? How about just ...

    A
    B
    C
    1
    3421112223334440000
    2
    B1: =SUMPRODUCT(--(A1 = $A$1:$A$8))
    2
    3421112223334440000
    2
    3
    3421112223334440001
    1
    4
    3421112223334440002
    1
    5
    3421112223334440010
    1
    6
    3421112223334440400
    1
    7
    3421112223334440009
    1
    8
    3421112223334440230
    1
    Entia non sunt multiplicanda sine necessitate

  13. #13
    Forum Contributor
    Join Date
    03-26-2015
    Location
    Cookeville, TN
    MS-Off Ver
    Excel 365
    Posts
    202

    Re: Lookup Long Reference Number?

    Quote Originally Posted by MrShorty View Post
    A quick internet search found this rundown on error handling when calling worksheet functions: https://rubberduckvba.wordpress.com/...on-and-errors/
    Thanks, that's very helpful! Even if I don't go the Application.Match route here, this is still useful information for future macros!

  14. #14
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,577

    Re: Lookup Long Reference Number?

    Try this alternate way
    Function
    =SUMPRODUCT(1*($C$1:$C$7=D5))
    Macro code

    Please Login or Register  to view this content.
    Attached Files Attached Files

+ 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. VBA to identify existing use of a reference number then locate next reference number?
    By Miskondukt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-05-2017, 04:36 PM
  2. Replies: 17
    Last Post: 05-16-2016, 01:22 AM
  3. Number format to Display 20 character long number.
    By SamCV in forum Excel General
    Replies: 6
    Last Post: 07-09-2014, 08:19 PM
  4. [SOLVED] =IF and =SUMIF formulas creating long long long data processing times.
    By comp in forum Excel Programming / VBA / Macros
    Replies: 32
    Last Post: 03-26-2014, 02:59 PM
  5. Find value and lookup row number to use as reference
    By woody83 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2012, 09:00 AM
  6. Reference Cells taking too long to load
    By dsrt16 in forum Excel General
    Replies: 0
    Last Post: 01-15-2009, 01:36 AM
  7. How to reference a different sheet with a long formula?
    By djarcadian in forum Excel General
    Replies: 5
    Last Post: 02-28-2008, 09:01 AM

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