?
Solved

how can i calculate the sum of numbers in a listbox?

Posted on 2003-02-26
7
Medium Priority
?
363 Views
Last Modified: 2011-08-18
Hi,

does anyone know how i can calculate the sum of several numbers in a listbox by simply clicking on a command button? The answer of the sum i want to vieuw in a textbox.

for example:

here is a list box:

     1
     2
     3

sum is a commandbutton and 6 is a textbox:

sum: 6

Thanks!
0
Comment
Question by:Soontjes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8024103
Hi Friend,

Try Out This Code,

Private Sub command1_click()
    Dim Sum, i As Integer
    For i = 0 To List1.ListCount - 1
        Sum = Sum + CInt(List1.List(i))
    Next
    MsgBox Sum
End sub
0
 

Expert Comment

by:tsugani
ID: 8025864
Its always a good idea when calculating sums (or products) to zero the sum before the 'for...next' loop, because the subroutine might be called more than once.
 
Therefore, add
sum=0
before
For i = 0 To List1.ListCount - 1
0
 
LVL 1

Accepted Solution

by:
sazhagianambi earned 80 total points
ID: 8030901
Yes, Absolutelu tsugani is Correct.

But In Vb If We declare as

Dim Sum

It Defaultly Contain the Value Zero
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:sazhagianambi
ID: 8030912
To Show the Result in TextBox

Text1.text=Sum

Regards,
Nambi
0
 

Expert Comment

by:CleanupPing
ID: 8901094
Soontjes:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8972941
Moderator, my recommended disposition is:

    Accept sazhagianambi's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question