# Need to locate the last instance of a string and then copy the row it appears on

Here's my problem.  I have a spreadsheet with two worksheets.  On sheet2, in cell B4 I have a value.  I need to get that value and then locate the  last instance of that value in column A on sheet1 and then copy the row it appears in  to   sheet2, row 10.  What's the most economical way to code this as a macro?

Any ideas?
example.xlsx
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You don't need a macro to do what you are looking for..You can do this with a formula..Enclosed is your workbook for your reference...

Saurabh...
example-1.xlsx
0
Author Commented:
Thanks Saurabh, while that's true, I need to be able to put this piece into a larger, pre-existing macro.
0
Commented:
You can use this code...

``````Sub copydata()
Dim rng As Range, cell As Range
Dim lrow As Long, str As String, r As Range
Dim ws As Worksheet, ws1 As Worksheet
Set ws = Sheets("Sheet1")
Set ws1 = Sheets("Sheet2")
str = ws1.Range("B4").Value
lrow = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A1:A" & lrow)
For Each cell In rng
If InStr(1, cell.Value, str, vbTextCompare) > 0 Then
Set r = ws.Range("A1:A" & cell.Row)
If Application.WorksheetFunction.CountIf(r, str) = Application.WorksheetFunction.CountIf(rng, str) Then
cell.EntireRow.Copy ws1.Range("a9")
End If
End If
Next cell

End Sub
``````

Saurabh...
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
check this code
``````Public Sub copylastfind()
Dim pLookupVal As String
pLookupVal = Sheet2.Cells(4, 2).Value
Dim ii As Integer
Dim mylastrow As Integer
mylastrow = 0
For ii = 1 To Sheet1.UsedRange.Rows.Count()
If UCase(Sheet1.Cells(ii, 1).Value) = UCase(pLookupVal) Then
mylastrow = ii
End If
Next
If mylastrow > 0 Then
Dim mylstrstr As String
mylstrstr = LTrim(RTrim(Str(mylastrow)))
Sheet1.Range(mylstrstr & ":" & mylstrstr).Rows.EntireRow.Copy Sheet2.Cells(10, 1)
End If

End Sub
``````
0
Data AnalystCommented:
Just adding another method to perform the check:
``````Sub EE_FindLastInstance()
Dim ws1 As Worksheet, ws2 As Worksheet, str As String, cell As Long
Application.ScreenUpdating = False

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")

str = UCase(ws2.Cells(4, 2).Value)
ws1.Activate
For cell = 1 To ws1.Cells(Rows.Count, 1).End(xlUp).Row
Cells(Rows.Count, 1).End(xlUp).Offset(1 - cell, 0).Select
If UCase(ActiveCell.Value) = str Then
Range(Selection, Selection.Offset(0, ws1.UsedRange.Columns.Count - 1)).Copy
ws2.Cells(10, 1).PasteSpecial xlPasteValues
Exit For
End If
Next cell
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
``````
0
Author Commented:
Thanks for all of the suggestions guys.  I ended up using Saurabh's solution as a framework on which to build the rest of my macro.  Thanks!
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.