Results 1 to 2 of 2

Thread: Advanced Filtering

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    62

    Advanced Filtering

    I'm in a bit of a pickle here...


    On sheet1, cell b3, I have a validated list of names.
    On sheet2, I have a range of data, ColA has the same names as cell b3 on sheet1, and col b has ID #'s.

    Sheet 2 looks something like this ::

    Bob 123
    Bob 231
    Bob 312
    Mary 423
    Mary 234
    Mary 342

    What I need to happen is when I select a name from the cell b3 on sheet1 is for all of the IDs that match that same name on sheet2 to populate onto sheet1 in the range b6:b30

    I understand this is an advanced filter function (don't know if it can be done i vba) but even trying various vlookup combinations, I can't get it to work and would greatly appreciate any assistance anyone could offer.

    "If at first you don't succeed, destroy all evidence you ever tried."
    http://www.ussretribution.com

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Advanced Filtering

    Albatross
    here is an event proce for your sheet1. It only executes the code when the name is changed. It removes the existing list of ID's and then writes the new list - starting in cell B6.
    VB Code:
    1. Private Sub Worksheet_Change(ByVal Target As Range)
    2.  
    3. Dim rngLookup As Range
    4. Dim rngOutput As Range
    5. Dim sUserName As String
    6. Dim lRowNum As Long
    7.     'Only run the code if the name is being changed
    8.     If Target.Address = "$B$3" Then
    9.        
    10.         'Disable Events to prevent repeat calls to
    11.         'the _Change event
    12.         Application.EnableEvents = False
    13.        
    14.         'Set range references
    15.         Set rngOutput = ActiveSheet.Range("B6")
    16.        
    17.         With Worksheets(2)
    18.             Set rngLookup = .Range(.Cells(2, 1), .Cells(2, 2).End(xlDown))
    19.         End With
    20.        
    21.         'Clear the Current Output
    22.         With rngOutput
    23.             'Special Hnadler for cases where only
    24.             'one ID is currently used
    25.             '(The .End Method won't work in this case)
    26.             If .Offset(1, 0).Value = "" Then
    27.                 .Value = ""
    28.             Else
    29.                 'All other cases - clear the output range
    30.                 'and all cells immediately under it
    31.                 .Resize(.End(xlDown).Row - .Row + 1, 1).Value = ""
    32.             End If
    33.         End With
    34.        
    35.         'Get the name of the user that has been selected
    36.         sUserName = Target.Value
    37.        
    38.         With rngLookup
    39.            
    40.             'Loop through the Lookup range
    41.             For lRowNum = 1 To .Rows.Count
    42.                
    43.                 'Looking for matching User Names
    44.                 'In the first column
    45.                 If .Cells(lRowNum, 1).Value = sUserName Then
    46.                    
    47.                     'Where a match is found - write the
    48.                     'ID to the output range and move down a row
    49.                     rngOutput.Value = .Cells(lRowNum, 2)
    50.                     Set rngOutput = rngOutput.Offset(1, 0)
    51.                 End If
    52.                
    53.             Next lRowNum
    54.            
    55.         End With
    56.        
    57.         'Re-enable events
    58.         Application.EnableEvents = True
    59.     End If
    60.    
    61.     'Clear Object Variables
    62.     Set rngLookup = Nothing
    63.     Set rngOutput = Nothing
    64. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width