Link to home
Start Free TrialLog in
Avatar of vbnewby
vbnewby

asked on

sum a multiline textbox

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)
ASKER CERTIFIED SOLUTION
Avatar of rdrunner
rdrunner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Guy Hengel [angelIII / a3]
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
>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.
Avatar of vbnewby
vbnewby

ASKER

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

Thanks guys

gotta love this site...


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, "+"))
Avatar of vbnewby

ASKER

sorry Rdrunner,

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