Extracting Various Data from a list with Same Criteria

tsroque

Board Regular
Joined
Jan 19, 2007
Messages
127
Office Version
  1. 365
I have a group of employees with their group number in an Excel worksheet. I need to individually group these employees under their Manager's ID in worksheet2.
Book2
ABCDEFGHI
1JanFebMar
2NameGroupIDMgrIDNameGroupIDMgrIDNameGroupIDMgrID
3Employee1Group1Mgr1Employee1Group1Mgr1Employee1Group1Mgr1
4Employee2Group1Mgr1Employee2Group1Mgr1Employee2Group1Mgr1
5Employee3Group3Mgr3Employee3Group3Mgr3Employee3Group3Mgr3
6Employee4Group1Mgr1Employee4Group1Mgr1Employee4Group1Mgr1
7Employee5Group2Mgr2Employee5Group2Mgr2Employee5Group2Mgr2
8Employee6Group3Mgr3Employee6Group3Mgr3Employee6Group3Mgr3
9Employee7Group3Mgr3Employee7Group3Mgr3Employee7Group3Mgr3
10Employee8Group2Mgr2Employee8Group2Mgr2Employee8Group2Mgr2
11Employee9Group3Mgr3Employee9Group3Mgr3Employee9Group3Mgr3
12Employee10Group1Mgr1Employee10Group1Mgr1Employee10Group1Mgr1
13Employee11Group2Mgr2Employee11Group2Mgr2Employee11Group2Mgr2
14Employee12Group3Mgr3Employee12Group3Mgr3Employee12Group3Mgr3
15Employee13Group1Mgr1Employee13Group1Mgr1Employee13Group1Mgr1
16Employee14Group2Mgr2Employee14Group2Mgr2
17Employee15Group2Mgr2Employee15Group2Mgr2
18Employee16Group3Mgr3
Sheet1


On Worksheet2, I set up a Validation list. When I choose a month, I want that corresponding information to filter on Worksheet2 and list the Employees with the same Mgr's ID.
Book2
ABCDEF
1MonthMar
2Mgr1Mgr2Mgr3
3NameGroupIDNameGroupIDNameGroupID
4Employee01Group1Employee05Group2Employee03Group3
5Employee02Group1Employee08Group2Employee06Group3
6Employee04Group1Employee11Group2Employee07Group3
7Employee10Group1Employee14Group2Employee09Group3
8Employee13Group1Employee15Group2Employee12Group3
9Employee16Group3
10
11
12
13
14
15
Sheet2


Yes...I know I could Auto Filter it, and then copy and paste it to a separate worksheet, but what's the fun in that if you can automate it! Keep in mind that new Mgrs could be added.

Thanks!
:D
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a, i As Long, b(), t As Long, x, w(), maxRow As Long
If Target.Address(0,0) <> "A1" Then Exit Sub
If IsError(Application.Match(Target.Value, Sheets("sheet1").Rows(1), 0)) Then
    MsgBox "data not found"
    Exit Sub
End If
Application.EnableEvents = False
Rows("2:" & Rows.Count).Clear
With Sheets("sheet2")
    a = .Range(.Cells(1, x), .Cells(Rows.Count, x).End(xlUp)).Resize(, 3).Value
End With
ReDim b(1 To UBound(a, 1), 1 To Columns.Count)
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For i = 3 To UBound(a, 1)
        If Not .exists(a(i, 3)) Then
            t = t + 2
            .add a(i, 3), VBA.Array(2, t)
            b(1, t - 1) = a(i, 3) : b(2, t - 1) = "Name"
            b(2, t) = "Group ID"
        End If
        w = .item(a(i, 3)): w(0) = w(0) + 1
        b(w(0), w(1) - 1) = a(i, 1) : b(w(0), w(1)) = a(i, 2)
        .item(a(i, 3)) = w
        maxRow = Application.Max(maxRow, w(0))
    Next
End With
If .count > 0 Then
    With Range("a2").Resize(maxRow, t)
        .Value = b
        .Rows(1).Interior.ColorIndex = 16
        .Rows(2).Interior.ColorIndex = 15
    End With
End If
End Sub
 
Upvote 0
AdvancedFilter's copy to other range will work.
However, the destination sheet must be active when AdvancedFilter is pressed, otherwise you get a misleading error message.
 
Upvote 0
Hi Seiya -

It gets stuck at:

If .count > 0 Then
With Range("a2").Resize(maxRow, t)
.Value = b
.Rows(1).Interior.ColorIndex = 16
.Rows(2).Interior.ColorIndex = 15
End With
End If
End Sub

And I'm assuming that when I change the validation selection in A2, it runs the formula?

Thanks Mike for your suggestion... I'll experiment with the AdvancedFilter tool as well, but it's still not quite automating my work...I'm lazy... ;)
 
Upvote 0

Forum statistics

Threads
1,215,475
Messages
6,125,028
Members
449,205
Latest member
Eggy66

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