+ Reply to Thread
Results 1 to 6 of 6

Extract Rows based on Cell Value

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2012
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Extract Rows based on Cell Value

    Hi

    I'm wanting to extract rows from Data sheet into Report sheet based on criteria in cell B1 of Report sheet and column E of Data sheet.

    eg. if Report sheet cell B1 = MLO I want to copy and paste all the Rows within Data sheet where column E contains MLO into the Report sheet (starting in cell C2)

    Thanks
    Andrew
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Extract Rows based on Cell Value

    Sub a()
    Set sh1 = Sheets("Data")
    Set sh2 = Sheets("Report")
    repo = sh2.Cells(1, 2)
    With sh1
    lastrow = .Range("A" & Rows.Count).End(xlUp).Row
    sh2initrow = 2
    sh2col = 3
    For j = 1 To lastrow
      If .Cells(j, 5).Value = repo Then
         .Range(.Cells(j, 1), .Cells(j, 6)).Copy sh2.Cells(sh2initrow, sh2col)
         sh2initrow = sh2initrow + 1
      End If
    Next
    End With
    End Sub

  3. #3
    Registered User
    Join Date
    07-23-2012
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Extract Rows based on Cell Value

    Thanks Patel

  4. #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!)

  5. #5
    Registered User
    Join Date
    07-23-2012
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Extract Rows based on Cell Value

    Thanks Leith. Just what I am after

  6. #6
    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,

    You're welcome.

+ Reply to Thread

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