Sum of numbers in a list box

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
90
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hello
I have a list box inside a user form and I want to display the total amount in the list box in a text box or label
For this I used the following code
VBA Code:
Private Sub sum_subjects()
Dim total_v As Double
    For i = 0 To ListBox1.ListCount - 1
        total_v = total_v + Val(ListBox1.Column(5, i))
        geo_v = geo_v + Val(ListBox1.Column(6, i))
    Next
        sum_total.Caption = total_v
        sum_geo.Caption = geo_v
End Sub
The problem is that when I type in the search form, it displays the total. How do I modify the code to display the search total (limited filtered)?
 

Attachments

  • 3333.png
    3333.png
    15.1 KB · Views: 8
Background photo correction
فاکتور _دو2.xlsm
I
13
Sheet1
 

Attachments

  • 3333.png
    3333.png
    21.7 KB · Views: 4
Upvote 0
Then each time you filter that list, you'll need to run that code. Show your other code if you need help calling that code.
 
Upvote 0
Solution
Then each time you filter that list, you'll need to run that code. Show your other code if you need help calling that code.
I need help calling that code. I would appreciate it if you could help.

The problem was solved by calling the code in the title.
 
Last edited:
Upvote 0
Post the code that controls the filter portion. Or post all your code for that form. Do you have a button to start the filter? Or does it filter automatically when you press enter in the Title textbox?
 
Upvote 0
Post the code that controls the filter portion. Or post all your code for that form. Do you have a button to start the filter? Or does it filter automatically when you press enter in the Title textbox?
This is the entire program code.

VBA Code:
Private Sub ComboBox1_Change()
Sheet1.Activate
Dim i
i = WorksheetFunction.CountA(Sheet1.Range("1:1")) + 1
Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(250, i)).Sort Key1:=Range("a1"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
  
Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(250, i)).Sort Key1:=Range("b1"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

End Sub


Private Sub CommandButton4_Click()
    Unload Me
End Sub


Private Sub TextBox8_Change()

    If ComboBox1.Text <> Empty Then Call serchTel
    If TextBox8.Text = Empty Then Call addlist
    sum_subjects
End Sub

Private Sub sum_subjects()
Dim math_v As Double
    For i = 0 To ListBox1.ListCount - 1
        math_v = math_v + Val(ListBox1.Column(3, i))
        geo_v = geo_v + Val(ListBox1.Column(4, i))
    Next
        sum_math.Caption = math_v
        sum_geo.Caption = geo_v
End Sub


Private Sub UserForm_Initialize()
    HideTitleBar Me
    'Me.cboSelect.List = WorksheetFunction.Transpose(Sheet1.Range("A1:G1"))
'  ÇäÏÇÒå íæÒÑ ÝÑã
    Me.Height = 250
    Me.Width = 415
    ListBox1.ColumnWidths = "1;20;70;60;50;50;60"
    Dim n
    n = 1
    Do While Sheet1.Cells(1, n) <> Empty
    ComboBox1.AddItem Sheet1.Cells(1, n).Value
    n = n + 1
    Loop
    Call addlist
    sum_subjects

End Sub

Public Sub addlist()
Dim n, col
n = 2

 col = WorksheetFunction.CountA(Sheet1.Range("1:1"))
 ListBox1.ColumnCount = col + 1
 ListBox1.Clear

Do While Sheet1.Cells(n, 1) <> Empty
           ListBox1.AddItem Sheet1.Cells(n, 6).Value  'n - 1 ' c.Row - 1
           For i = 0 To col
            ListBox1.List(ListBox1.ListCount - 1, i + 1) = Sheet1.Cells(n, i + 1).Value ' c.Value
           Next i

n = n + 1
Loop
End Sub

Private Sub serchTel()
Dim col, i

col = WorksheetFunction.CountA(Sheet1.Range("1:1"))
 ListBox1.ColumnCount = col + 1
 ListBox1.Clear
 With Sheet1.Range(Sheet1.Cells(2, ComboBox1.ListIndex + 1), Sheet1.Cells(1000, ComboBox1.ListIndex + 1)) '("a1:a10000")

    Set c = .Find(TextBox8.Text, LookIn:=xlValues, LookAt:=xlPart)
  
    If Not c Is Nothing Then
        firstAddress = c.Address
      
        Do
        'n = n + 1
        'Stop
           ListBox1.AddItem Sheet1.Cells(c.Row, 6).Value  '  n ' c.Row - 1
           For i = 0 To col
            ListBox1.List(ListBox1.ListCount - 1, i + 1) = Sheet1.Cells(c.Row, i + 1).Value ' c.Value
           Next i
        Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If

End With
End Sub

I can't upload the file here.

 
Upvote 0
It looks like it is working for you. If you wanted to make a change so that when you press enter in the Title Textbox the form updated rather than each time the Title textbox changes do this:


Replace this
VBA Code:
Private Sub TextBox8_Change()

    If ComboBox1.Text <> Empty Then Call serchTel
    If TextBox8.Text = Empty Then Call addlist
    sum_subjects
End Sub

With this
VBA Code:
Private Sub TextBox8_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
  If KeyCode = 13 Then
    If ComboBox1.Text <> Empty Then Call serchTel
    If TextBox8.Text = Empty Then Call addlist
    sum_subjects
  End If
End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top