Results 1 to 6 of 6

Extract Rows based on Cell Value

Threaded View

  1. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Extract Rows based on Cell Value

    Hello AndrewS6,

    Welcome to the Forum!

    I expanded what you asked for. Since B1's content is controlled by a validation drop down, the macro will fill the range starting at C2 with the row data. The macro is called automatically whenever B1 changes. This is accomplished by the Worksheet Change event for the Report worksheet. The macros are listed below. The attached workbook contains these changes.

    Extract Rows Macro
    Sub ExtractRows()
      
        Dim Cell As Range
        Dim DstRng As Range
        Dim FindWhat As Variant
        Dim LastCol As Long
        Dim r As Long
        Dim RngEnd As Range
        Dim SrcRng As Range
        
            Set SrcRng = Worksheets("Data").Range("E1")
            Set DstRng = Worksheets("Report").Range("C2")
            
            FindWhat = Worksheets("Report").Range("B1")
            
                LastCol = SrcRng.Parent.Cells(1, Columns.Count).End(xlToLeft).Column
                
                Set RngEnd = SrcRng.Parent.Cells(Rows.Count, SrcRng.Column).End(xlUp)
                If RngEnd.Row < SrcRng.Row Then Exit Sub Else Set SrcRng = SrcRng.Parent.Range(SrcRng, RngEnd)
                
                    Set DstRng = DstRng.Resize(ColumnSize:=LastCol + DstRng.Column - 1)
                
                    Set RngEnd = DstRng.Parent.Cells(Rows.Count, DstRng.Column).End(xlUp)
                    Set DstRng = IIf(RngEnd.Row < DstRng.Row, DstRng, DstRng.Parent.Range(DstRng, RngEnd))
                
                    DstRng.ClearContents
                    
                    For Each Cell In SrcRng
                        If Cell = FindWhat Then
                            CellRow = Cell.Offset(0, -4).Resize(ColumnSize:=LastCol).Value
                            DstRng.Offset(r, 0).Resize(1, LastCol).Value = CellRow
                            r = r + 1
                        End If
                    Next Cell
                
    End Sub

    Report Worksheet Change Event MAcro
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        If Target.Cells.Count > 1 Then Exit Sub
        
        If Intersect(Target, Range("$B$1")) Is Nothing Then Exit Sub
        
            Application.EnableEvents = False
                Call ExtractRows
            Application.EnableEvents = True
            
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1