Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
DB DB is offline
external usenet poster
 
Posts: 46
Default recognizing double digit numbers in code

i am currently working a code to add the numerical parts of strings such as
"M4G3 M4P0". A problem arises however in my code when the string contains a
double digit number(ex. M4G3 M12P0). My code will recognize this as a two
individual single digit numbers (12= 1 and 2). Any suggestions? Here is the
code I'm using.
**********
Sub Sumcharacters()
Dim i As Long, s As String
Dim lsum As Long
For i = 1 To Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
If IsNumeric(s) Then
lsum = lsum + CLng(s)
End If
Next
ActiveCell.Offset(0, 1).Value = lsum
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default recognizing double digit numbers in code

try this

Dim i As Long, s As String
Dim lsum As Long
i = 1
while i <= Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
Isum = 0
if IsNumeric(s) then

while IsNumeric(s)
lsum = (10 *lsum) + CLng(s)
i = i + 1
loop
else
i = i + 1
end if
loop
ActiveCell.Offset(0, 1).Value = lsum
End Sub

"DB" wrote:

i am currently working a code to add the numerical parts of strings such as
"M4G3 M4P0". A problem arises however in my code when the string contains a
double digit number(ex. M4G3 M12P0). My code will recognize this as a two
individual single digit numbers (12= 1 and 2). Any suggestions? Here is the
code I'm using.
**********
Sub Sumcharacters()
Dim i As Long, s As String
Dim lsum As Long
For i = 1 To Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
If IsNumeric(s) Then
lsum = lsum + CLng(s)
End If
Next
ActiveCell.Offset(0, 1).Value = lsum
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.misc
DB DB is offline
external usenet poster
 
Posts: 46
Default recognizing double digit numbers in code

When I use this, I get an error message for an invalid outside procedure. It
highlights "i=1" in the third line with the error message

"Joel" wrote:

try this

Dim i As Long, s As String
Dim lsum As Long
i = 1
while i <= Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
Isum = 0
if IsNumeric(s) then

while IsNumeric(s)
lsum = (10 *lsum) + CLng(s)
i = i + 1
loop
else
i = i + 1
end if
loop
ActiveCell.Offset(0, 1).Value = lsum
End Sub

"DB" wrote:

i am currently working a code to add the numerical parts of strings such as
"M4G3 M4P0". A problem arises however in my code when the string contains a
double digit number(ex. M4G3 M12P0). My code will recognize this as a two
individual single digit numbers (12= 1 and 2). Any suggestions? Here is the
code I'm using.
**********
Sub Sumcharacters()
Dim i As Long, s As String
Dim lsum As Long
For i = 1 To Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
If IsNumeric(s) Then
lsum = lsum + CLng(s)
End If
Next
ActiveCell.Offset(0, 1).Value = lsum
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default recognizing double digit numbers in code

I sent an updated verion of this file in a second posting. Use the newer
program because it has some improvements. compile the program before you use
it. In VBA window under debug select compile. check for compiling errrors.
I = 1 as no reference by itself to a outside procedure. Ther must be other
compiling errors.

"DB" wrote:

When I use this, I get an error message for an invalid outside procedure. It
highlights "i=1" in the third line with the error message

"Joel" wrote:

try this

Dim i As Long, s As String
Dim lsum As Long
i = 1
while i <= Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
Isum = 0
if IsNumeric(s) then

while IsNumeric(s)
lsum = (10 *lsum) + CLng(s)
i = i + 1
loop
else
i = i + 1
end if
loop
ActiveCell.Offset(0, 1).Value = lsum
End Sub

"DB" wrote:

i am currently working a code to add the numerical parts of strings such as
"M4G3 M4P0". A problem arises however in my code when the string contains a
double digit number(ex. M4G3 M12P0). My code will recognize this as a two
individual single digit numbers (12= 1 and 2). Any suggestions? Here is the
code I'm using.
**********
Sub Sumcharacters()
Dim i As Long, s As String
Dim lsum As Long
For i = 1 To Len(ActiveCell)
s = Mid(ActiveCell, i, 1)
If IsNumeric(s) Then
lsum = lsum + CLng(s)
End If
Next
ActiveCell.Offset(0, 1).Value = lsum
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I fix loosing 1st digit in zip code in mail merge? Lori Excel Worksheet Functions 2 April 28th 10 03:06 AM
Recognizing 2-digit years Dallman Ross Excel Discussion (Misc queries) 12 December 10th 06 08:50 PM
12 digit code sorting Mark Excel Worksheet Functions 5 October 31st 05 10:21 PM
Recognizing Numbers or names as duplicates texansgal Excel Worksheet Functions 1 August 8th 05 08:31 PM
16 DIGIT NUMBERS IN CELL WITHOUT LAST DIGIT BEING A ZERO jnkell Excel Worksheet Functions 2 December 18th 04 07:13 PM


All times are GMT +1. The time now is 09:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"