How to sum of a Listbox Colum

In this tutorial we will discuss the vba codes which is used in this module for sum of a listbox column.First of all we need to put error handling code so that if you face any error then it will run the next command.

On Error Resume Next

 Next is for variable we need to set the variable as double when we find out the sum then double is a very significant variable.

Dim sum1, sum2, sum3, sum4 As Double

Now need a integer to find out the total number or number of data present in the listbox. And visit this integer such a way so that we can some every row for the column.

For r = 0 To Listbox1.ListCount - 1

Here Listbox1.ListCount is to count total item in the listbox1.

sum1 = 0

sum2 = 0

sum3 = 0

sum4 = 0

The value of the above variable is to set zero because the initial value will be zero.

Now we use to some for every value of the column of listbox as below

  sum1 = sum1 + .List(r, 11)

  sum2 = sum2 + .List(r, 13)

  sum3 = sum3 + .List(r, 15)

  sum4 = sum4 + .List(r, 16) 

  Next r

Here 11 13 15 16 are the column number of the listbox when and we count the column number we need to start from 0 that is the first column of the listbox will be zero and then count.

Now we set the textbox value as described in the video tutorial by the variables sum1, sum2, sum3 and sum4 as follow

Userform1.TextBox2.Value = sum1

Userform1.TextBox3.Value = sum2

Userform1.TextBox4.Value = sum3

Userform1.TextBox5.Value = sum4

I hope I have been able to present the matter clearly to you. . If you have any questions please write on comment box. Thank you.

Private Sub CommandButton1_Click()

 On Error Resume Next

Dim sum1, sum2, sum3, sum4 As Double

Dim r As Integer

sum1 = 0

sum2 = 0

sum3 = 0

sum4 = 0

With Userform1.ListBox1

  For r = 0 To .ListCount - 1

  sum1 = sum1 + .List(r, 11)

  sum2 = sum2 + .List(r, 13)

  sum3 = sum3 + .List(r, 15)

  sum4 = sum4 + .List(r, 16) 

  Next r

End With

Userform1.TextBox2.Value = sum1

Userform1.TextBox3.Value = sum2

Userform1.TextBox4.Value = sum3

Userform1.TextBox5.Value = sum4

End Sub

2 comments:

  1. very nice video & useful for me. Is there any tutorial about printing data from listbox to printer output then please share.

    ReplyDelete
  2. it is very nice and wonder work .also it is useful please keep in sharing
    such a wonderful idea thanks a lot.

    ReplyDelete