Sum amount for selected item in listbox

S

SF

Hi,

I have a listbox on a form that allow multiple selection of row. The
rowsourcde of the listbox has 2 column, ID and AmountPaid (Currency).

I want to sum the AmountPaid of the selected item within a listbox. How can
I acheive that?

SF
 
K

Ken Snell \(MVP\)

Loop through the ItemsSelected property of the listbox and add up the
values; note that the values will be text and you'll need to convert back to
numeric to ensure valid result:

Dim varItemSel As Variant
Dim dblSum As Double
dblSum = 0
For Each varItemSel In Me.NameOfListBox.ItemsSelected
dblSum = dblSum + CDbl(Me.NameOfListBox.Column(2,varItemSel)
Next varItemSel
MsgBox dblSum
 
K

Ken Snell \(MVP\)

Drat -- lost a parenthesis somewhere.

Here is corrected code snippet:

Dim varItemSel As Variant
Dim dblSum As Double
dblSum = 0
For Each varItemSel In Me.NameOfListBox.ItemsSelected
dblSum = dblSum + CDbl(Me.NameOfListBox.Column(2,varItemSel))
Next varItemSel
MsgBox dblSum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top