Macro to find last occurance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is the code I have so far; however instead of finding the first
occurrence of what was entered into the input box, I need the last occurrence
in column C. Can anyone help?

Sub Insertinfo()
Dim FindWhat As String
Dim FoundCell As Range

FindWhat = InputBox("Find What", "Find")
If StrPtr(FindWhat) = 0 Then
Exit Sub
End If
On Error Resume Next
Set FoundCell = Range("c:c").Find(What:=FindWhat, _
LookAt:=xlWhole, LookIn:=xlValues)
If FoundCell Is Nothing Then
MsgBox "Not Found"
Else
FoundCell.Activate
With ActiveCell.EntireRow
.Copy
.Insert Shift:=xlDown
End With
End If
Application.CutCopyMode = False
End Sub

Thanks,
Holly
 
Try this

If you have more then one occurrence of the value this will
select the last occurrence in column C.

Sub Find_Last()
Dim FindString As String
Dim rng As Range
FindString = InputBox("Enter a Search value")
If Trim(FindString) <> "" Then
With Sheets("Sheet1").Range("C:C")
Set rng = .Find(What:=FindString, _
After:=.Cells(1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False)
If Not rng Is Nothing Then
Application.Goto rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
 
Thank you much, especially for the quick response! It worked beautifully.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top