View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default Macro to search every row in sheet2 then display all matching

Another approach that you might use it to apply an autofilter to your data
and then copy the filtered data.

You might look at Ron de Bruin's site and get some ideas on how to code the
autofilter (and of course the macro recorder can be useful as well)

http://www.rondebruin.nl/copy5.htm

--
Regards,
Tom Ogilvy




"Ryan Hess" wrote:

Thank you Tom!

This worked for searching numerical values!

The problem I am now having is, I need to be able to do the search for Text
and Dates.

When I try entering either of those I get a "Type Mismatch" error. Not sure
where in the code you provided I need to modify it so that it will search
properly.

Thank you again!

"Tom Ogilvy" wrote:

Assume the item to search for in column A is in Cell B9 of Sheet1 and you
want the found rows to be pasted starting in B10 of Sheet1.

Sub GetData()
Dim sh As Worksheet
Dim sh1 As Worksheet
Dim sAddr As String, s As Variant
Dim rng As Range, rng1 As Range
Set sh1 = Worksheets("Sheet1")
Set sh = Worksheets("Sheet2")
s = sh1.Range("B9")
Set rng = sh.Range(sh.Range("A2"), _
sh.Cells(Rows.Count, "A")).Find(What:=s, _
After:=sh.Cells(Rows.Count, 1), _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If Not rng Is Nothing Then
sAddr = rng.Address
Do
If rng1 Is Nothing Then
Set rng1 = rng
Else
Set rng1 = Union(rng1, rng)
End If
Set rng = sh.Range(sh.Range("A2"), _
sh.Cells(Rows.Count, 1)).FindNext(rng)
Loop Until rng.Address = sAddr
If Not rng1 Is Nothing Then
Set rng1 = Intersect(rng1.EntireRow, sh.Range("A:E"))
rng1.Copy sh1.Range("B10")
End If
End If
End Sub

--
Regards,
Tom Ogilvy

"Ryan Hess" wrote:

My macro button is on sheet1.

On sheet2 is an ever growing database of information. There are 5 different
column values and the information is grouped by row.

(not sure that explains it well)

I want the macro button on sheet1 to initiate a search of the database on
sheet2.

I want it to search all the contents of column A (excluding A1)
For every cell that matches the search I want to select that row and copy
the 5 column values for that row onto sheet1.

Haven't been able to figure this one out.

Any help would be greatly appreciated.

Thank you!
Ryan