Filter based on group and populate into listbox

JIB

New Member
Joined
Dec 24, 2019
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone. This is a continuation to the filter I brought up before. So I got it to work, filtering based on individual,

Filter.png

but now what i'm trying to do is filter based on groups. Here is what I mean:

Filter 2.png


What I'm trying to do is have each individual filtered under grade 19 populated in the listbox only once, than have the total amount of training and training hours summed up.
However, I'm not quite sure how to go about this. I thought of doing this through countifs (code below is only countif)

VBA Code:
Private Sub cmbGred_Change()
Sheet1.Activate

Me.ListBox1.Clear
Me.ListBox1.AddItem
For a = 1 To 3
Me.ListBox1.List(0, a - 1) = Sheet1.Cells(1, a)
Me.ListBox1.List(0, 3) = "Total amount of Training"
Me.ListBox1.List(0, 4) = "Total Training hours"
Next a

Dim My_range As Integer
Dim Column As Byte

For k = 2 To Sheet1.Range("A100000").End(xlUp).Offset(1, 0).Row
c = Application.WorksheetFunction.CountIf(Sheet1.Range("b" & k), Me.cmbGred)
If CStr(Sheet1.Cells(k, 2)) = CStr(Me.cmbGred) And c = 1 Then
'if cell matches combobox value and if duplicate is found  via count if
Me.ListBox1.AddItem
My_range = My_range + 1
For Column = 1 To 3
Me.ListBox1.List(My_range, Column - 1) = Sheet1.Cells(k, Column)

D = cmbGred.Value
Me.ListBox1.List(1, 3) = Application.WorksheetFunction.CountIf(Sheet1.Range("A:A"), D)

Next Column

End If
Next k

End Sub

However, I realized the filter criteria is based on the combobox value, which only refers to the grade. In this case, i'm not sure how to refer to the name cell (as it runs through the loop).

I also tried adding c = c + 1, code runs but with no difference. Appreciate all help I can get. Please and thank you.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
So I managed to figure out how to sum up the total amount of training per person however, I still cant figure out how to properly populate the listbox without duplicates

VBA Code:
Private Sub cmbGred_Change()
Sheet1.Activate

Me.ListBox1.Clear
Me.ListBox1.AddItem
For a = 1 To 3
Me.ListBox1.List(0, a - 1) = Sheet1.Cells(1, a)
Me.ListBox1.List(0, 3) = "Total amount of Training"
Me.ListBox1.List(0, 4) = "Total Training hours"
Next a

On Error Resume Next

Dim My_range As Integer
Dim Column As Byte


For k = 2 To Sheet1.Range("A100000").End(xlUp).Offset(1, 0).Row
d = Sheet1.Range("b" & k).Offset(0, -1)


If CStr(Sheet1.Cells(k, 2)) = CStr(Me.cmbGred) Then
Me.ListBox1.AddItem
My_range = My_range + 1
For Column = 1 To 3
Me.ListBox1.List(My_range, Column - 1) = Sheet1.Cells(k, Column)

F = Application.WorksheetFunction.CountIf(Sheet1.Range("A:A"), d)
Me.ListBox1.List(My_range, 3) = F

Next Column
If Me.ListBox1.List(My_range - 1, Column - 2).Value = Me.ListBox1.List(My_range, Column - 1) Then
Exit Sub
End If
End If
Next k



End Sub

This code stops the whole thing entirely once a entry matches the previous one, so all I get is one name and entry in the list box.
VBA Code:
Next Column
If Me.ListBox1.List(My_range - 1, Column - 2).Value = Me.ListBox1.List(My_range, Column - 1) Then
Exit Sub
End If
End If
Next k

Is there something I can replace exit sub with so that I can skip to the next loop? Would appreciate some help. Please and thank you.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,909
Members
452,949
Latest member
beartooth91

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