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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
What are the possible combinations of input format? (IE: value1 and value2, value1 & value2, value1 value2, etc.) Are the values limited to single words?
 
Upvote 0
What are the possible combinations of input format? (IE: value1 and value2, value1 & value2, value1 value2, etc.) Are the values limited to single words?
only one = 'and' sun AND dark, or green AND tree. I want to be able to type any two words with the word 'and' between and the code, if
correct should find all cells in a column that CONTAIN both words.
 
Upvote 0
Give this change a try:
VBA Code:
Dim x, y As String
   x = Textbox9.Value
   y = Textbox10.Value

to:
VBA Code:
Dim x As String, y As String, arr() As String
arr() = Split(Replace(Textbox9.Value, " and ", " "), " ")
x = arr(0)
y = arr(1)
 
Upvote 0
A little confused, so i made an image of the form. Description explains. I'm not sure where your corrected code fits into
the code block I originally sent. Textbox1 at the top is the way I'd like to make it work - with just a single textbox containing both terms or words.

cr
 

Attachments

  • Textbox1 at the top is how it should be.  Texbox9(value1) and Textbox10(value2) is the way it ...png
    Textbox1 at the top is how it should be. Texbox9(value1) and Textbox10(value2) is the way it ...png
    91.7 KB · Views: 3
Upvote 0
A little confused, so i made an image of the form. Description explains. I'm not sure where your corrected code fits into
the code block I originally sent. Textbox1 at the top is the way I'd like to make it work - with just a single textbox containing both terms or words.

cr
It replaces the lines of your code I showed in #4:
VBA Code:
Dim x, y As String
   x = Textbox9.Value
   y = Textbox10.Value
Your lines of code ^

to:
VBA Code:
Dim x As String, y As String, arr() As String
arr() = Split(Replace(Textbox1.Value, " and ", " "), " ")
x = arr(0)
y = arr(1)
Replace with my lines of code ^

You are typing into Textbox1 and not Textbox9?
 
Upvote 0
Correct. When the code works as it should, Textboxes 9 and 10 will be permanently deleted. Too much clutter on the form for me. I like
to keep things as simple as possible. Traditional applications allow any single word - "green":, phrase - "last days",
two words separated between text - "the quick "brown" fox jumped over the "fence". If I type in brown fence just like this in Textbox1, with no
quotes or commas, it should pick up every cell that contains these two words or any two words, whether next to each other
or separated by other text. If punctuation is needed to make it work, no problem.
That way, every word form can be found.

BTW - thanks for all your help on this.
cr
 
Upvote 0
Did you replace the lines of code I indicated with what I suggested?
 
Upvote 0
..yes. After posted my reply, i followed your directions - and it works great!
Thanks again, for all your help.
cr :)
 
Upvote 0
You're welcome and happy to help. It will also accept two words without "and" between.
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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