Finding any two values in each cell text in a column of cells with both values as one Textbox1 variable

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
884
Office Version
  1. 365
Platform
  1. Windows
As the title explains This code below works well enough if two variables x and y from two different textboxes are used:
Code:
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    Dim cell As Range
    Dim targetRow, lastrow As Long
    Dim x, y As String
   x = Textbox9.Value
   y = Textbox10.Value
    Set wsSource = ThisWorkbook.Sheets("Source") ' Change to your source sheet name
     Sheets("Result").UsedRange.ClearContents
    Set wsTarget = Sheets("Result") 'ThisWorkbook.Sheets.Add ' Creates a new sheet for the filtered data
    targetRow = 1 ' Initialize the target row
    For Each cell In wsSource.Range("E1:E31103")      'UsedRange
        If InStr(1, cell.Value, x, vbTextCompare) > 0 And InStr(1, cell.Value, y, vbTextCompare) > 0 Then
           cell.EntireRow.Copy wsTarget.Cells(targetRow, 1)
            targetRow = targetRow + 1
       End If
    Next cell
    MsgBox "Filtered data copied to Result sheet"
    lastrow = Sheets("Result").Range("B" & rows.count).End(xlUp).Row
    Me.TextBox3.Value = ListBox2.ListIndex + 1
    Me.TextBox4.Value = lastrow
    Sheets("Result").Range("H1").Value = lastrow
End Sub
[/ccode]

This line looks for the two variable values x and y from two different textboxes, Textbox9 and Textbox10.
[code]
If InStr(1, cell.Value, x, vbTextCompare) > 0 And InStr(1, cell.Value, y, vbTextCompare) > 0 Then...
I don't want two textboxes. I want to be able to type both values in one textbox
such as
Code:
x = Textbox1.value if Textbox1.value = "sun [and] moon" , OR "green [and] tree"
and give the same result as having each value  in two separate textboxes
I'm having a tough time figuring out how to change the InStr code line to do that 

Any help would be appreciated. 
cr
 
...one more thing. The beauty about this revised code is that will also find just about any word form:
In case you haven't guessed, this is a Bible application. Any search to find words or phrases has to be done
over 31,103 rows of verse text across 4 columns. The button stays depressed for quite while. Anyway, all I'll ever need to do in any search is
type in Textbox1 any of these forms. I did and the new code found them all:

"sun dark" - any two words that has text between, before or after them.
"last days" - any single phrase
"Isaiah 65:" - any Book, chapter
"Matthew 24:15" - any Book, chapter and verse

...without quotes, of course.

Thanks again, dreid1011

cr
 
Upvote 0

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

Forum statistics

Threads
1,215,076
Messages
6,122,984
Members
449,092
Latest member
Mr Hughes

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