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
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