VBA - How to find words from list in a cell?

Fixed

Board Regular
Joined
Apr 28, 2017
Messages
95
Hello!

I have the function:
Code:
IF(SUMPRODUCT(--ISNUMBER(SEARCH(E1:E2,A1)));"yes","no")
that searches the text values from E1:E2 in A1 cell.
How to make the same in VBA?

Thanks in advance!
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
Function FindTxts(a As Range, r As Range) As Variant
Dim x$
Set r = [r].Find([a])
x = IIf(r Is Nothing, "No", "Yes")
FindTxts = x
End Function

It works, thank you!
 
Upvote 0
If a = a1, r = E1:E2 then I guess it searches A1 in E1:E2, but I need E1:E2 in A1.
 
Upvote 0
Code:
Sub ft()
Dim rng As Range, s$, cel As Range, c%
Set rng = [E1:E2]
s = UCase([A1])
For Each cel In rng
    If InStr(1, s, UCase(cel)) <> 0 Then
        c = 1
        Exit For
    End If
Next
MsgBox IIf(c = 1, "Yes", "No")
End Sub
 
Upvote 0
See if this function works for you...
Code:
[table="width: 500"]
[tr]
	[td]Function FindTxts(SingleCell As Range, RangeOfCells As Range) As Variant
  Dim Cel As Range
  For Each Cel In RangeOfCells
    FindTxts = FindTxts + (InStr(1, SingleCell, Cel.Value, vbTextCompare) > 0)
  Next
  If FindTxts Then FindTxts = "Yes" Else FindTxts = "No"
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Code:
Sub ft()
Dim rng As Range, s$, cel As Range, c%
Set rng = [E1:E2]
s = UCase([A1])
For Each cel In rng
    If InStr(1, s, UCase(cel)) <> 0 Then
        c = 1
        Exit For
    End If
Next
MsgBox IIf(c = 1, "Yes", "No")
End Sub

Now it works as I expected, thank you!
 
Upvote 0
See if this function works for you...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Function FindTxts(SingleCell As Range, RangeOfCells As Range) As Variant
  Dim Cel As Range
  For Each Cel In RangeOfCells
    FindTxts = FindTxts + (InStr(1, SingleCell, Cel.Value, vbTextCompare) > 0)
  Next
  If FindTxts Then FindTxts = "Yes" Else FindTxts = "No"
End Function[/TD]
[/TR]
</tbody>[/TABLE]

Great function, Rick, thank you!
 
Upvote 0
When E1 or E2 is empty, the functions say "yes" instead of "no".
How to find the non-empty values only?
I think I should use Ubound to find the last non-empty value, but I don't know how.
Please help me.
Thank you.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
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