Addition to code for find/search text based on cell value

stroffso

Board Regular
Joined
Jul 12, 2016
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have the below code which finds text based on what is in cell D1. I have a button connected to this code but what i want to happen is when the user presses the button once its finds the first instance, then if the press again it goes to the 2nd and so on down. Currently it just finds the first instance and nothing else.

I get that you can just use CTRL + F for the same thing but there is a reason I want it to be coded, thanks in advance


Sub search()


Dim FindString As String
Dim Rng As Range
FindString = Range("D1")
If Trim(FindString) <> "" Then
With Sheets("Data").Range("B:B")
Set Rng = .Find(what:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Application.GoTo Rng, True
Else
MsgBox "Nothing found"
End If
End With
End If
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
In the future, please use code tags to make it easier for us to read.

Try this, modifications highlighted in red.
Rich (BB code):
Sub DoSearch()
  Static rngAfter As Excel.Range

  Dim FindString As String
  Dim Rng As Range
  
  FindString = Range("D1")
  
  If rngAfter Is Nothing Then
    Set rngAfter = Sheets("Data").Range("B:B").Cells(1)
  End If
  
  If Trim(FindString) <> "" Then
    With Sheets("Data").Range("B:B")
      Set Rng = .Find(what:=FindString, _
              After:=rngAfter, _
              LookIn:=xlValues, _
              LookAt:=xlWhole, _
              SearchOrder:=xlByRows, _
              SearchDirection:=xlNext, _
              MatchCase:=False)
    End With
    If Not Rng Is Nothing Then
      Application.GoTo Rng, True
      Set rngAfter = Rng
    Else
      MsgBox "Nothing found"
    End If
  End If
End Sub
 
Last edited:
Upvote 0
Thank you so much that has worked wonderfully. When you say use code tags I did try this but obviously have not done it right, apologies
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top