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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Totaling a list box full of numbers

Status
Not open for further replies.

jKin

Programmer
Mar 19, 2009
3
0
0
CA
Hi,

I would like to total a list box full of numbers and put this total in a text box. I have tried this code, but when I run it I get a “type mismatch” error.

For i = 1 To List4.ListCount
x = x + CInt(List4.List(i))
Next
txtTotals(0).Text = x

when I take the CInt off, it adds up the numbers but puts them in a string format: 3502550, instead of adding 350+25+50

Thanks very much for any help!!!
 
Try declaring your variables as integers. Note that the array that holds the listbox contents is zero-based.
Code:
  Dim i As Integer
  Dim x As Integer
  
  For i = 0 To (List4.ListCount - 1)
    x = x + CInt(List4.List(i))
  Next

  txtTotals.Text = x
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top