Link to home
Start Free TrialLog in
Avatar of military donut
military donutFlag for United States of America

asked on

Search Workbook, then move rows to new sheet

Hello,

I was trying to find a way to search an entire workbook, then move the entire row containing that search to a new worksheet.

Is there any way to do this by VBA?
Avatar of Mike in IT
Mike in IT
Flag of United States of America image

You would probably want something like this. That is the VBA find. That will allow you to find whatever you are looking for in a cell. Then once you find it select the whole row and copy it to where you want.
Avatar of Joe Howard
To search an entire WorkBook you will have to loop through the sheets one at a time preforming the search on each one.

Something like this should do it:
Dim sht As WorkSheet, shtNew As WorkSheet
Dim cell As Range

Set shtNew = ThisWorkbook.Worksheets.Add(, ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
On Error Resume Next
shtNew.Name = "Whatever Name You Want"
On Error Goto 0

For Each sht In ActiveWorkbook.Worksheets
    If sht.Name <> shtNew.Name Then
        Set cell = sht.Cells.Find("Whatever You want to Find")
        cell.EntireRow.Copy Destination:=shtNew.Range("A" & shtNew.Range("A" & Rows.Count).End(xlUp).Row)
    End If
Next

Open in new window

Avatar of military donut

ASKER

so here is what I am using...and get the error

Object Variable or with block variable not set

Dim sht As Worksheet
Dim Cell As Range

For Each sht In ActiveWorkbook.Worksheets
    Set Cell = sht.Cells.Find("W911N2-12-D-0040")
    Cell.EntireRow.Copy Destination:=Sheets(1).Range("A" & Sheets(1).Range("A" & Rows.Count).End(xlUp).Row)
    
Next

Open in new window

I also get the same with:

Sub FindAddress()
With ActiveWorkbook.Worksheets
    Dim sht As Worksheet
    Dim Cell As Range
    Set Cell = sht.Cells.Find("W911N2-12-D-0040")
        Do
        Cell.EntireRow.Copy Destination:=Sheets(1).Range("A" & Sheets(1).Range("A" & Rows.Count).End(xlUp).Row)
           
        Loop While Not Cell Is Nothing
End With

End Sub

Open in new window

On which line do get the error?
on:

Cell.EntireRow.Copy Destination:=Sheets(1).Range("A" & Sheets(1).Range("A" & Rows.Count).End(xlUp).Row)
Change the code to this where line 6 checks to see if anything was found.

Dim sht As Worksheet
Dim Cell As Range

For Each sht In ActiveWorkbook.Worksheets
    Set Cell = sht.Cells.Find("W911N2-12-D-0040")
    If Not Cell Is Nothing Then
        Cell.EntireRow.Copy Destination:=Sheets(1).Range("A" & Sheets(1).Range("A" & Rows.Count).End(xlUp).Row)
    End If
Next

Open in new window

does work, but should be 23 records but only get one copied into the sheet
It's copying all 23 to the same row, Where do you want them to go? One under the other?
yes...
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great.  Works perfect.  

You have been patient with me all day...thanks so much!
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015