Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summarizing the content of a cell in Excell

Status
Not open for further replies.

Buitre71

Technical User
Mar 13, 2003
2
US
Hi,

How can I summarize the digits of a number from an Excel cell?

If I have 25252525 in A1, how can I get the sum (28) in B1 using code?

At this time I'm using the code shown below, but I would like to avoid creating columns which I have to delete later.

Thanks in advance for your help.



Sub Test()

'Text to columns

Selection.TextToColumns Destination:=ActiveCell, DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array _
(5, 1), Array(6, 1), Array(7, 1), Array(8, 1))

ActiveCell.Offset(0, 8).Range("A1").Select

'Formula

ActiveCell.FormulaR1C1 = _
"=RC[-8]+RC[-7]+RC[-6]+RC[-5]+RC[-4]+RC[-3]+RC[-2]+RC[-1]"

End Sub
 
paste the following function in a module...


Function add_digits(in_cell) As Long
Do While Len(in_cell) > 0
x = Left(in_cell, 1)
add_digits = add_digits + x
in_cell = Right(in_cell, Len(in_cell) - 1)
Loop
End Function


then on your spreadsheet in b1 type: =add_digits(a1)
 
Or, using ETID's function:

Sum doEmAll()
For each c in activesheet.range("B1:B" & range("B65536").end(xlup).row)
c.value = add_digits(c)
next
end sub Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top