USING FIND METHOD WITH A VARIABLE TO FIND A STRING

Status
Not open for further replies.

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
Office Version
  1. 365
Platform
  1. Windows
There are many FIND examples out there and they all work great - for finding ONE VALUE. There are more
times I want to find a phrase or string value, not just one word. The code I have below, familiar to all works well
for finding just a single word.

Code:
Private Sub cmdFINDVERSE_Click()
Sheets("RESULT").UsedRange.ClearContents
Dim x As String
Dim c As Range
Dim rw As Long
Dim firstaddress As Variant
Dim rowno As Integer
x = Me.TextBox6.Value  --->Textbox6 can be any single word or string value, either between quotes or not
With Worksheets("Sheet2").Range("H1:H31103")
Set c = .Find(x, LookIn:=xlValues, LookAt:=xlPart)
If Not c Is Nothing Then
rw = 1
firstaddress = c.Address
Do
Worksheets("Sheet2").Select
Range(Cells(c.Row, 5), Cells(c.Row, 9)).Copy Destination:=Sheets("RESULT").Range("A" & rw)
rw = rw + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstaddress
End If
End With
rowno = Sheets("RESULT").Range("A1").End(xlDown).Row
Sheets("RESULT").Range("G1").Value = rowno
Sheets("RESULT").Range("H1").Value = x
Me.TextBox7.Value = Sheets("RESULT").Range("G1").Value
ListBox2.ListIndex = 0
SEARCHRESULTS.Show
End Sub
Instead of typing the single word "appointed"(without quotes) in Textbox6 - which finds all occurrences
of the word appointed, how would I tell Excel to find ANY phrase or string, like "appointed time of the end",
or "latter days", or "sickness unto death", etc. The phrase or string can be in quotes but does not have to be.
Whatever makes the FIND more accurate.

This seems simple enough. I'm having trouble changing the code above to make sure it works with a phrase
or string, as mentioned. The two small screenshots below explain clearly what I'd like.

Thanks very much for anyone's help.
cr
 

Attachments

  • PHRASE WITHOUT QUOTES.jpg
    PHRASE WITHOUT QUOTES.jpg
    11.2 KB · Views: 18
  • PHRASE WITH QOUTES.jpg
    PHRASE WITH QOUTES.jpg
    14.3 KB · Views: 17

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What you have should work if you don't add the quotes to your search string.
If you wanted to play it safe you could add to the find string ,MatchCase:=False, SearchFormat:=False
but if they were your issue you would have the same issue with the single word search.

Maybe test your search string manually and then copy paste it into your text box to make sure it the exact same phrase.
Additional spaces or other small variations in the phrase would stop it being found.
 
Upvote 0
Hi Alex -
...everything works well just by typing in a phrase or string without any quotes.
I added the additional parameters as you suggested above. I'm confident
this code will FIND and display just about anything thing I type in.

Thanks for all your help.
cr
Kingwood, Tx
 
Upvote 0
Any chance you can post the full working code without any extra lines or constraints. This looks like what I need
 
Upvote 0
Any chance you can post the full working code without any extra lines or constraints. This looks like what I need
Duplicate to: VBA: Find a number of phrases in a sheet and highlight the row as long as one phrase is in it

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this old thread so please continue in the linked thread. If you do not receive a response, you can "bump" it by replying to it yourself, though we advise you to wait 24 hours before doing so, and not to bump a thread more than once a day.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,852
Messages
6,127,313
Members
449,374
Latest member
analystvar

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