Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-02-26
7
Medium Priority
?
375 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
6 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

581 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