Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default adding numbers with letters

How do you add numbers in a cell with letters mixed?
such as:

160grn+4wht+17grn

Thanx in advance for your help!
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,836
Default adding numbers with letters

Try this UDF:

Function SumCharacters(Rng As Range) As Long

Dim i As Long
Dim s As String
Dim lSum As Long
Dim mycell As Range

lSum = 0
For Each mycell In Rng.Cells
For i = 1 To Len(mycell.Value)
s = Mid(mycell.Value, i, 1) 'mycell.text if it's formatted
If IsNumeric(s) Then
lSum = lSum + s
End If
Next i
Next mycell

SumCharacters = lSum
End Function



Regards,
Ryan--


--
RyGuy


" wrote:

How do you add numbers in a cell with letters mixed?
such as:

160grn+4wht+17grn

Thanx in advance for your help!

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,202
Default adding numbers with letters

You UDF adds each digit individually... looking at the structure of the
string, it seems more likely to me that the OP want to throw away the
letters and add the numbers with plus signs between them. I'm thinking a UDF
more like this would be what the OP wants...

Function SumCharacters(Rng As Range) As Double
Dim X As Long
Dim Y As Long
Dim Addends() As String
If Rng.Count = 1 Then
Addends = Split(Replace(Rng.Value, "-", "+-"), "+")
For X = 0 To UBound(Addends)
For Y = 1 To Len(Addends(X))
If Mid$(Addends(X), Y, 1) Like "[!0-9.-]" Then
Addends(X) = Left$(Addends(X), Y - 1)
Exit For
End If
Next
SumCharacters = SumCharacters + CDbl(Addends(X))
Next
End If
End Function

Note: I set this UDF up so it would calculate the number whether a plus or
minus sign separated them.

Rick


"ryguy7272" wrote in message
...
Try this UDF:

Function SumCharacters(Rng As Range) As Long

Dim i As Long
Dim s As String
Dim lSum As Long
Dim mycell As Range

lSum = 0
For Each mycell In Rng.Cells
For i = 1 To Len(mycell.Value)
s = Mid(mycell.Value, i, 1) 'mycell.text if it's formatted
If IsNumeric(s) Then
lSum = lSum + s
End If
Next i
Next mycell

SumCharacters = lSum
End Function



Regards,
Ryan--


--
RyGuy


" wrote:

How do you add numbers in a cell with letters mixed?
such as:

160grn+4wht+17grn

Thanx in advance for your help!


  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default adding numbers with letters

On Jan 8, 12:42 pm, "Rick Rothstein \(MVP - VB\)"
wrote:
You UDF adds each digit individually... looking at the structure of the
string, it seems more likely to me that the OP want to throw away the
letters and add the numbers with plus signs between them. I'm thinking a UDF
more like this would be what the OP wants...

Function SumCharacters(Rng As Range) As Double
Dim X As Long
Dim Y As Long
Dim Addends() As String
If Rng.Count = 1 Then
Addends = Split(Replace(Rng.Value, "-", "+-"), "+")
For X = 0 To UBound(Addends)
For Y = 1 To Len(Addends(X))
If Mid$(Addends(X), Y, 1) Like "[!0-9.-]" Then
Addends(X) = Left$(Addends(X), Y - 1)
Exit For
End If
Next
SumCharacters = SumCharacters + CDbl(Addends(X))
Next
End If
End Function



I'm new to functions. I havent tried this because i need to make sure
i got it right.

I need this so i can print in different cell, (ie- "3wht+170grn" in
cell C1 which would show as "173" in cell C2.)

Anything I need to change in this to do that, and how do I add the
function? Just add it in with an equal sign in front?

I feel so stupid, and appreciate your time?
  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,202
Default adding numbers with letters

You UDF adds each digit individually... looking at the structure of the
string, it seems more likely to me that the OP want to throw away the
letters and add the numbers with plus signs between them. I'm thinking a
UDF
more like this would be what the OP wants...

Function SumCharacters(Rng As Range) As Double
Dim X As Long
Dim Y As Long
Dim Addends() As String
If Rng.Count = 1 Then
Addends = Split(Replace(Rng.Value, "-", "+-"), "+")
For X = 0 To UBound(Addends)
For Y = 1 To Len(Addends(X))
If Mid$(Addends(X), Y, 1) Like "[!0-9.-]" Then
Addends(X) = Left$(Addends(X), Y - 1)
Exit For
End If
Next
SumCharacters = SumCharacters + CDbl(Addends(X))
Next
End If
End Function


I'm new to functions. I havent tried this because i need to make sure
i got it right.

I need this so i can print in different cell, (ie- "3wht+170grn" in
cell C1 which would show as "173" in cell C2.)

Anything I need to change in this to do that, and how do I add the
function? Just add it in with an equal sign in front?


From the spreadsheet, press Alt+F11 to get into the VBA editor. Then click
on Insert/Module (that is Module, not Class Module) in the menu bar.
Copy/Paste the function I posted into the code window that opened up when
you click the Insert/Module menu item. That's it; you can know use the
SumCharacters function as if it were a built-in Excel function. If the text
to add is in C1 as you posted, then put this in C2...

=SumCharacters(C1)

and it should give you 173 as the answer. By the way, I used the name for
the function that Ryan used, but that is not a requirement. If you wanted to
call the function by a different name, then just change the 3 occurrences of
SumCharacters in my code to whatever name you want to use.

Rick



  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default adding numbers with letters

Whoops

Apologies Rick.......was not paying attention and missed your response, which I
duplicated later.


Gord

On Tue, 8 Jan 2008 17:13:54 -0500, "Rick Rothstein \(MVP - VB\)"
wrote:

You UDF adds each digit individually... looking at the structure of the
string, it seems more likely to me that the OP want to throw away the
letters and add the numbers with plus signs between them. I'm thinking a
UDF
more like this would be what the OP wants...

Function SumCharacters(Rng As Range) As Double
Dim X As Long
Dim Y As Long
Dim Addends() As String
If Rng.Count = 1 Then
Addends = Split(Replace(Rng.Value, "-", "+-"), "+")
For X = 0 To UBound(Addends)
For Y = 1 To Len(Addends(X))
If Mid$(Addends(X), Y, 1) Like "[!0-9.-]" Then
Addends(X) = Left$(Addends(X), Y - 1)
Exit For
End If
Next
SumCharacters = SumCharacters + CDbl(Addends(X))
Next
End If
End Function


I'm new to functions. I havent tried this because i need to make sure
i got it right.

I need this so i can print in different cell, (ie- "3wht+170grn" in
cell C1 which would show as "173" in cell C2.)

Anything I need to change in this to do that, and how do I add the
function? Just add it in with an equal sign in front?


From the spreadsheet, press Alt+F11 to get into the VBA editor. Then click
on Insert/Module (that is Module, not Class Module) in the menu bar.
Copy/Paste the function I posted into the code window that opened up when
you click the Insert/Module menu item. That's it; you can know use the
SumCharacters function as if it were a built-in Excel function. If the text
to add is in C1 as you posted, then put this in C2...

=SumCharacters(C1)

and it should give you 173 as the answer. By the way, I used the name for
the function that Ryan used, but that is not a requirement. If you wanted to
call the function by a different name, then just change the 3 occurrences of
SumCharacters in my code to whatever name you want to use.

Rick


  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 3
Default adding numbers with letters

On Jan 8, 5:13*pm, "Rick Rothstein \(MVP - VB\)"
wrote:
You UDF adds each digit individually... looking at the structure of the
string, it seems more likely to me that the OP want to throw away the
letters and add the numbers with plus signs between them. I'm thinking a
UDF
more like this would be what the OP wants...


Function SumCharacters(Rng As Range) As Double
* Dim X As Long
* Dim Y As Long
* Dim Addends() As String
* If Rng.Count = 1 Then
* * Addends = Split(Replace(Rng.Value, "-", "+-"), "+")
* * For X = 0 To UBound(Addends)
* * * For Y = 1 To Len(Addends(X))
* * * * If Mid$(Addends(X), Y, 1) Like "[!0-9.-]" Then
* * * * * Addends(X) = Left$(Addends(X), Y - 1)
* * * * * Exit For
* * * * End If
* * * Next
* * * SumCharacters = SumCharacters + CDbl(Addends(X))
* * Next
* End If
End Function


I'm new to functions. I havent tried this because i need to make sure
i got it right.


I need this so i can print in different cell, (ie- "3wht+170grn" in
cell C1 which would show as "173" in cell C2.)


Anything I need to change in this to do that, and how do I add the
function? Just add it in with an equal sign in front?


From the spreadsheet, press Alt+F11 to get into the VBA editor. Then click
on Insert/Module (that is Module, not Class Module) in the menu bar.
Copy/Paste the function I posted into the code window that opened up when
you click the Insert/Module menu item. That's it; you can know use the
SumCharacters function as if it were a built-in Excel function. If the text
to add is in C1 as you posted, then put this in C2...

=SumCharacters(C1)

and it should give you 173 as the answer. By the way, I used the name for
the function that Ryan used, but that is not a requirement. If you wanted to
call the function by a different name, then just change the 3 occurrences of
SumCharacters in my code to whatever name you want to use.

Rick- Hide quoted text -

- Show quoted text -


Thanx, you guys are great! Worked fine after tweaking security
settings.

One last question, sometimes the addends are fractions such as:

88 1/2grn+3wht+88 1/2grn

How do you tweak the code to see these?
  #8   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 22,906
Default adding numbers with letters

The function code itself must be copied/pasted into a general module in the
workbook.

Alt + F11 to open VBEditor.

CTRL + r to open Project Explorer.

Right-click on your workbook/project and InsertModule

Paste the UDF into that module.

Alt + q to return to Excel

Then in any cell enter =sumcharacters(cellref) where cellref is the cell
with 3wht+170grn


Gord Dibben MS Excel MVP

On Tue, 8 Jan 2008 13:48:04 -0800 (PST), wrote:

I'm new to functions. I havent tried this because i need to make sure
i got it right.

I need this so i can print in different cell, (ie- "3wht+170grn" in
cell C1 which would show as "173" in cell C2.)

Anything I need to change in this to do that, and how do I add the
function? Just add it in with an equal sign in front?

I feel so stupid, and appreciate your time?


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
adding numbers but not letters SoozeeC Excel Worksheet Functions 4 December 20th 07 11:00 AM
Adding a space inbetween letters and numbers in a cell Jazzman10 Excel Discussion (Misc queries) 2 June 21st 07 01:31 PM
Help adding letters in column melisa0820 Excel Worksheet Functions 2 June 11th 07 02:28 PM
adding numbers with letters Singha Excel Discussion (Misc queries) 4 October 25th 06 01:34 PM
FORMULAS FOR ADDING LETTERS REPRESENTING NUMBERS koolone Excel Discussion (Misc queries) 1 April 8th 05 12:39 PM


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

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

About Us

"It's about Microsoft Excel"