mercredi 25 février 2015

Finding multiple values and copying the whole row in a new sheet

I have a excel table with user info. There are 1000 rows and a few columns (name, address, email...). What I would like to do is make a macro that would do the following: Search the whole table with the values (street name) I input and when there is a match copy the whole row into another sheet. There would be multiple values inputted.


So far I've been using this:



Option Explicit
Sub GeneFinder()

Dim srchLen, gName, nxtRw As Integer

Dim g As Range
'Clear Sheet 2 and Copy Column Headings
Sheets(2).Cells.ClearContents
Sheets(1).Rows(1).Copy Destination:=Sheets(2).Rows(1)
'Determine length of Search Column from Sheet3
srchLen = Sheets(3).Range("A" & Rows.Count).End(xlUp).Row
'Loop through list in Sheet3, Column A. As each value is
'found in Sheet1, Column D, copy it top the next row in Sheet2
With Sheets(1).Columns("D")
For gName = 2 To srchLen
Set g = .Find(Sheets(3).Range("A" & gName), lookat:=xlWhole)
If Not g Is Nothing Then
nxtRw = Sheets(2).Range("D" & Rows.Count).End(xlUp).Row + 1
g.EntireRow.Copy Destination:=Sheets(2).Range("A" & nxtRw)
End If
Next
End With
End Sub`


Sheet 3 is where i input the names of the streets I need inside column A starting from A2, sheet 2 is where the rows should be copied and column D inside sheet 1 is where all the street names are.


Thanks.


Aucun commentaire:

Enregistrer un commentaire