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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Saurabh Singh TeotiaCommented:
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...

dcmathisAuthor Commented:
Thanks Saurabh, while that's true, I need to be able to put this piece into a larger, pre-existing macro.
Saurabh Singh TeotiaCommented:
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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
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

Open in new window

Rodney EndrigaData 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)
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

Open in new window

dcmathisAuthor 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!
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.