#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default wrap text

Try this:

Function SumValuesInCell(WrappedText As String) As Double
Dim Vals As Variant
Dim sum As Double, i As Long

WrappedText = Replace(WrappedText, vbCr, "")
Vals = Split(WrappedText, vbLf)
For i = 0 To UBound(Vals)
sum = sum + Val(Trim(Vals(i)))
Next i
SumValuesInCell = sum
End Function

Function SumAll(R As Range) As Double
Dim cl As Range
Dim sum As Double
For Each cl In R.Cells
sum = sum + SumValuesInCell(cl.Value)
Next cl
SumAll = sum
End Function

Enter this into a general code module. Then in your example,
=SumAll(A1:A4) used as a user-defined spreadsheet function (put say in
A5) will yield 3991 (as it should). It could also be called in another
macro via something like SumAll(Range("A1:A4"))

This code works in Windows. It will also work in MAC OS X and above. If
for some reason you are using Excel in Macs with an older operating
system this code won't work because of the way those systems handled
newlines.

Hope that helps

-John Coleman

bijan wrote:
I have a huge data that some of them contain wrap text cells with number value,
how I can sum this example correctly with macro?
A B
1 10
20
2 54
3 3365
18
4 524


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 44
Default wrap text

Thank you John

"John Coleman" wrote:

Try this:

Function SumValuesInCell(WrappedText As String) As Double
Dim Vals As Variant
Dim sum As Double, i As Long

WrappedText = Replace(WrappedText, vbCr, "")
Vals = Split(WrappedText, vbLf)
For i = 0 To UBound(Vals)
sum = sum + Val(Trim(Vals(i)))
Next i
SumValuesInCell = sum
End Function

Function SumAll(R As Range) As Double
Dim cl As Range
Dim sum As Double
For Each cl In R.Cells
sum = sum + SumValuesInCell(cl.Value)
Next cl
SumAll = sum
End Function

Enter this into a general code module. Then in your example,
=SumAll(A1:A4) used as a user-defined spreadsheet function (put say in
A5) will yield 3991 (as it should). It could also be called in another
macro via something like SumAll(Range("A1:A4"))

This code works in Windows. It will also work in MAC OS X and above. If
for some reason you are using Excel in Macs with an older operating
system this code won't work because of the way those systems handled
newlines.

Hope that helps

-John Coleman

bijan wrote:
I have a huge data that some of them contain wrap text cells with number value,
how I can sum this example correctly with macro?
A B
1 10
20
2 54
3 3365
18
4 524



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default wrap text

You're welcome.
An optimization: the Trim() function is playing no role whatsoever. I
often use it when dealing with string input as a form of defensive
programming but in this case it is defending against nothing since
Val() seems to be indifferent to any leading or trailing whitespace (as
a bit of experimenting in the immediate window shows). Thus the line
sum = sum + Val(Trim(Vals(i)))
could be replaced by
sum = sum + Val(Vals(i))
without changing the functionality. It could make a slight but
noticable difference in calculation time if used with large data sets.

Happy New Year

-John Coleman

bijan wrote:
Thank you John


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 get date/time to wrap (format - wrap text doesn't work)? Alex Excel Discussion (Misc queries) 3 April 4th 23 02:29 PM
Excel 2007; even though "Wrap Text" is on the text does wrap? Bruce M[_2_] Excel Discussion (Misc queries) 0 December 31st 09 02:58 PM
Wrap text doesn't wrap [email protected] Excel Worksheet Functions 1 March 28th 07 01:58 AM
Why won't text in cell wrap. Cell format set to wrap. DRB Excel Discussion (Misc queries) 2 September 28th 06 08:27 PM
Text not continuing to wrap for large block of text in Excel cell Mandra Charts and Charting in Excel 1 May 15th 06 07:13 PM


All times are GMT +1. The time now is 01:08 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"