Sum of items selected in a list box

mendesj1

Registered User.
Local time
Today, 15:58
Joined
Nov 9, 2011
Messages
30
Just some vba help i need to know how to get the sum of column 2 of a list box
total bags is in the second column, i only want the total of bags of the ones selected

I can get the sum of all the boxes but only want highlighted ones thanks so much


Public Function SumListBox(sForm As String, _
sCtrl As String, iColumn As Integer) As Variant

Dim frm As Form
Dim ctrl As Control
Dim i As Integer
Dim vSum As Variant

On Error Resume Next
Set frm = Forms(sForm)

If Err <> 0 Then
SumListBox = "ERR!FormNotFound"
Exit Function
End If

On Error Resume Next
Set ctrl = frm(sCtrl)
If Err <> 0 Then
SumListBox = "ERR!ListboxNotFound"
Exit Function
End If

If iColumn > ctrl.ColumnCount Then
SumListBox = "ERR!ColumnNotFound"
Exit Function
End If

vSum = 0

For i = 0 To ctrl.ListCount - 1

vSum = vSum + ctrl.Column(iColumn - 1, i)
Next i

SumListBox = vSum

End Function
Private Sub Text10_Click()
Me.Text10 = SumListBox([NAME], [List2].[NAME], 2)


End Sub
 
Use the list box's Selected property to test if the row is selected (true) or not (false).
 

Users who are viewing this thread

Back
Top Bottom