ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA to identify last digit in integer (https://www.excelbanter.com/excel-programming/362595-vba-identify-last-digit-integer.html)

brucemc[_8_]

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?


--
brucemc
------------------------------------------------------------------------
brucemc's Profile: http://www.excelforum.com/member.php...o&userid=32871
View this thread: http://www.excelforum.com/showthread...hreadid=546104


Ron Rosenfeld

VBA to identify last digit in integer
 
On Fri, 26 May 2006 21:07:59 -0500, brucemc
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

bgeier[_23_]

VBA to identify last digit in integer
 

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


--
bgeier
------------------------------------------------------------------------
bgeier's Profile: http://www.excelforum.com/member.php...o&userid=12822
View this thread: http://www.excelforum.com/showthread...hreadid=546104


brucemc[_9_]

VBA to identify last digit in integer
 

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...


--
brucemc
------------------------------------------------------------------------
brucemc's Profile: http://www.excelforum.com/member.php...o&userid=32871
View this thread: http://www.excelforum.com/showthread...hreadid=546104


bgeier[_25_]

VBA to identify last digit in integer
 

Microsoft uses "string" differently depending on what they are trying t
say.
In code "String" means a string of characters, regardless of how human
perceive the characters.

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

--
bgeie
-----------------------------------------------------------------------
bgeier's Profile: http://www.excelforum.com/member.php...fo&userid=1282
View this thread: http://www.excelforum.com/showthread.php?threadid=54610


AA2e72E

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..





All times are GMT +1. The time now is 03:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com