Link to home
Start Free TrialLog in
Avatar of Soontjes
Soontjes

asked on

how can i calculate the sum of numbers in a listbox?

Hi,

does anyone know how i can calculate the sum of several numbers in a listbox by simply clicking on a command button? The answer of the sum i want to vieuw in a textbox.

for example:

here is a list box:

     1
     2
     3

sum is a commandbutton and 6 is a textbox:

sum: 6

Thanks!
Avatar of sazhagianambi
sazhagianambi

Hi Friend,

Try Out This Code,

Private Sub command1_click()
    Dim Sum, i As Integer
    For i = 0 To List1.ListCount - 1
        Sum = Sum + CInt(List1.List(i))
    Next
    MsgBox Sum
End sub
Its always a good idea when calculating sums (or products) to zero the sum before the 'for...next' loop, because the subroutine might be called more than once.
 
Therefore, add
sum=0
before
For i = 0 To List1.ListCount - 1
ASKER CERTIFIED SOLUTION
Avatar of sazhagianambi
sazhagianambi

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
To Show the Result in TextBox

Text1.text=Sum

Regards,
Nambi
Soontjes:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
Avatar of DanRollins
Moderator, my recommended disposition is:

    Accept sazhagianambi's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer