+ Reply to Thread
Results 1 to 6 of 6

VBA to identify last digit in integer

  1. #1
    Registered User
    Join Date
    03-27-2006
    Posts
    70

    VBA to identify last digit in integer

    I need to identify the last digit in a test integer.

    I presently am about to convert the integer to a string, use Right(TestInteger,1) to extract the last character, then convert that character back to a value. Seems to me I, once again, am probably missing some other painfully obvious and easier means.

    Any ideas?

  2. #2
    Ron Rosenfeld
    Guest

    Re: VBA to identify last digit in integer

    On Fri, 26 May 2006 21:07:59 -0500, brucemc
    <[email protected]> wrote:

    >
    >I need to identify the last digit in a test integer.
    >
    >I presently am about to convert the integer to a string, use
    >Right(TestInteger,1) to extract the last character, then convert that
    >character back to a value. Seems to me I, once again, am probably
    >missing some other painfully obvious and easier means.
    >
    >Any ideas?


    TestInteger Mod 10


    This seems to work in VBA:

    ==========================
    Sub LastDigit()
    Const Num As Long = 79368412

    Debug.Print "Last Digit in " & Num & " is " & _
    Num Mod 10
    End Sub

    ===========================


    --ron

  3. #3
    Forum Contributor
    Join Date
    08-07-2004
    Location
    Ohio, USA
    Posts
    114
    Why convert to string, just test the numeric value itself

    debug.print right(12345,1)
    debug.print right(intValue,1)

    either way works with out converting to a string

  4. #4
    Registered User
    Join Date
    03-27-2006
    Posts
    70
    Darn it, and Thank-YOU!

    I've got to quit taking what MS writes as fact, unless there is a "special" definition they were using for "String" in the description of the RIGHT function...

  5. #5
    Forum Contributor
    Join Date
    08-07-2004
    Location
    Ohio, USA
    Posts
    114
    Microsoft uses "string" differently depending on what they are trying to say.
    In code "String" means a string of characters, regardless of how humans perceive the characters.

    Whenever Microsoft uses "string" in a description of a function, they mean whatever they want it to mean, usually it means any type of character.

  6. #6
    AA2e72E
    Guest

    RE: VBA to identify last digit in integer

    The solution to your specific problem has been identified already.

    This is an interesting problem. The number of digits in the integer part of
    any number is given by:

    int(1+log(YourNumber)/log(10))

    where YourNumber can be an integer or float. Say YourNumber is 1236.45, the
    last digit is:

    Debug.Print mid(1236.45,int(1+log(1236.45)/log(10)),1)

    This can be extended; say, you wanted the last but one digit, the position is:

    int(1+log(YourNumber)/log(10)) -1

    etc..




+ 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