sum a multiline textbox

vbnewby
vbnewby used Ask the Experts™
on
As you can tell from my signon name, I am only new to VB (do a little VBA, Thought I would uprgrade.), but I have a problem,  I have a multiline text box for the user to add a list of numbers. How do I get VB to add these numbers up?  its probably something simple that I have missed (Can get it to work in VBA by using a Spreadsheet)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hello,

To sum the numbers in a multi line textbox you would need to split them up and then add them

To split them up you can use

dim myArray() as string
myArray=split(myTextbox.text, vbcrlf)

Now you got 1 string per line in your array (starting at 0) not you can add them in a loop

dim nCounter,nSum

for nCounter = 0 to ubound(myArray)
if isnumeric(myArray(nCounter)) then nSum = nSum + cdbl(myArray(nCounter))
next

Hope this helps
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
Does each line have 1 single value?
if yes, split up the contents by the separator vbCrLf, and then add the items up:

dim strLines() as string
dim lngLoop as long
dim lngvalue as Long
dim lngTotal as long
strLines = Split(yourTextbox.text, vbCrlf)
lngTotal= 0
for  lngLoop = lbound(strLines) to ubound(strLines)
  lngValue = val(strLines(lngLoop))
  lngTotal = lngTotal + lngValue
next

msgbox lngTotal

CHeers

Commented:
>its probably something simple that I have missed

Not really.  Excel has functionality built in to perform summations, but VB is a general-purpose tool designed generically meaning that you have to do special-purpose things like this yourself.

The comments above should handle it.  However, these will only work with VB6/Office 2000 VBA.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Thanks RD runner,  almost had it myself, I missed the vbcrlf in the split.

Thanks guys

gotta love this site...


Commented:
No Problem, glad it works now ...

If you like this site, then grade the answer you like please ;)

Thanx

How about a single line of code?

Add the Miscrosoft Script control as a component reference and add it to your form.  Then perform this single line:

MsgBox ScriptControl1.Eval(Replace(Text1.Text, vbCrLf, "+"))

Author

Commented:
sorry Rdrunner,

thought I had passed these on to you....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial