Filter listbox data with specific value, sum the result and show in textbox

Rolsu

New Member
Joined
Jul 16, 2019
Messages
27
Office Version
  1. 2021
Platform
  1. Windows
Hi all!

I have a issue with my register.
I have data in a 4 column listbox. In the fourth I have hours and in the third I have the worktype (can be two types). I'd like to have the sum hours of the two types and show them in two textboxes.
I tried to filter the listbox and sum the selected data but in this case the listbox content always changed and I don't want that.
So I moved to a differnt method.
I have a code that filters the data on a sheet where the listbox gets the data. I also have a function in a standard module that sums the visible cells. I'd like to call the function in the filter code but can't refer properly.
Can someone help?
Or if someone can offer me an easier solution that's ok too.
Thanks.

Filter code:

VBA Code:
    Dim Code As String
    Dim myDB As Range

    If TextBox1.Value < 0 Then Exit Sub
        Code = TextBox1.Value

    With Sheets("Data")
        Set myDB = Sheets("Data").Range("A1:D1").Resize(.Cells(.Rows.Count, 1).End(xlUp).Row)
    End With

    With myDB
        .AutoFilter 'remove filters
        .AutoFilter field:=1, Criteria1:=Code  'filter data
        .AutoFilter field:=3, Criteria1:="Design"
        Call SumVisible(???????) 'here I always get the ByRef argument error
        .AutoFilter
    End With

Sum code:
VBA Code:
Function SumVisible(WorkRng As Range) As Double
    
    Dim rng As Range
    Dim total As Double

        For Each rng In WorkRng
            If rng.Rows.Hidden = False And rng.Columns.Hidden = False Then
            total = total + rng.Value
            End If
        Next
        
    TextBox2.Value = total
    
End Function
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You have the listbox list items coming from a filtered range? What about looping over the listbox items along these lines?
VBA Code:
If Listbox1.column(2,i) = "type1" then 
   Textbox1 = Textbox1 +Listbox1.Column(3,i)
Else 'or do a specific test for "type2"
   Textbox2 = Textbox2 + Listbox1.Column(3,i)
End If
Column property is zero based, so column 2 is the 3rd column.
 
Upvote 0
Solution
You have the listbox list items coming from a filtered range? What about looping over the listbox items along these lines?
VBA Code:
If Listbox1.column(2,i) = "type1" then
   Textbox1 = Textbox1 +Listbox1.Column(3,i)
Else 'or do a specific test for "type2"
   Textbox2 = Textbox2 + Listbox1.Column(3,i)
End If
Column property is zero based, so column 2 is the 3rd column.
Hi Micron!

Sorry for the late reply.
I've tried your code but can't get the sum value of type1, only just the header of the 4th column...
 
Upvote 0
I don't understand what you're saying, especially since I can't see what you see. My suggestion was to loop over the listbox items instead of the sheet. If you used exactly what I posted, that is not a loop; it's how to get at the column values. However, I think it matters if your listbox is on a sheet or userform. I seem to be able to get at column values when on a form, but not when on a sheet but you have not disclosed where yours is.
 
Upvote 0
I don't understand what you're saying, especially since I can't see what you see. My suggestion was to loop over the listbox items instead of the sheet. If you used exactly what I posted, that is not a loop; it's how to get at the column values. However, I think it matters if your listbox is on a sheet or userform. I seem to be able to get at column values when on a form, but not when on a sheet but you have not disclosed where yours is.
I don't use any userform, my list-and textboxes are on a worksheet.
And yes, the problem was with the direct use of your code. I did the loop but forgot to change some things.
After the modifications it works perfectly.
Here is the full code:
VBA Code:
    Dim i As Integer
    Dim total As Double
       
    With ListBox1
        For i = 0 To .ListCount - 1
            If ListBox1.Column(2, i) = "Design" Then
                total = total + ListBox1.Column(3, i)
            End If
        Next i
    End With
   
    TextBox2.Value = total
Thank you for the help!
 
Upvote 0
Glad I was able to help & thanks for the recognition.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,974
Members
449,095
Latest member
Mr Hughes

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