Sum Listbox items

  • Thread starter Thread starter Soniya
  • Start date Start date
S

Soniya

hI ALL,

Is it possible to sum the items in a listbox and get it
to be the caption of a label. even if the numbers are
stored as text and added to the listbox?

TIA

Soniya
 
This procedure will handle pure text items with an error
message and will sum numbers in a double data type. You
didn't specify what kind of numbers they were. Change as
necessary.


Sub SumListBoxItems()
On Error Resume Next
Dim dblSum As Double, dblVar As Double
For i = 0 To ListBox1.ListCount - 1
dblVar = CDbl(ListBox1.List(i))
If Err.Number <> 0 Then
If Err.Number <> 13 Then
MsgBox Err.Number & " - " & Err.Description
' Post if this happens, I'm curious.
Exit Sub
Else
' Put something else / additional here if
you want to
' do something with the text items in the
listbox.
MsgBox ListBox1.List(i) & " is not a valid
number."
Err.Clear
End If
Else
dblSum = dblSum + dblVar
End If
Next
Label1.Caption dblSum
End Sub
 

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

Access Cannot select items in listbox 1
refresh listbox 2
Refresh Listbox 2
Blank item in listbox, how to remove 3
Wrapping entries in a ListBox 1
Re Post Please help: Listboxes question 4
Excel listbox 2
Listbox ? 1

Back
Top