-
Hi i have a list box that gets items added to it, the items that are added are numbers. Once i have finished adding the numbers i need a function to total the listbox numbers up.
Please note i need this to work in any number of items within the list box.
Thanks for all you help.
James
-
Code:
Private Function listsum(list As ListBox)
Dim i As Integer
Dim sum As Long
For i = 0 To list.ListCount
sum = sum + Val(list.list(i))
Next i
listsum = sum
End Function
Usage
Code:
dim sum as long
sum = listsum(list1)
-
Dont understand?
Hi sorry could you write it in a command button for me im not sure what usage etc is?
Thanks
Private Sub cmd1_Click()
what goes in here?
End Sub
-
Total newbie, a. No problem. Put this into the general section of the code.
Code:
Private Function listsum(list As ListBox)
Dim i As Integer
Dim sum As Long
For i = 0 To list.ListCount
sum = sum + Val(list.list(i))
Next i
listsum = sum
End Function
Under button click event
Code:
Private Sub cmd1_Click()
dim sum as long
sum = listsum(nameofyourlistbox)
End Sub
-
Or if you don't want to use a function:
Code:
Private Sub cmd1_Click()
Dim i As Integer
Dim sum As Long
For i = 0 To list.ListCount
sum = sum + Val(list.list(i))
Next i
'sum contains sum
End Sub
-
Brilll!!!!!!
Thats great i was totalling the list every time something was added to not not all in one go. Your a star!!! I love VB and i have only been learning it at university a week!!!
James:cool: