Function if cell contains "text1" and not "text2"

Temporary-Failure

Board Regular
Joined
Jul 16, 2010
Messages
140
I have tried to make function which is like =takeif("text1";"text2";range)
text1 is what should be found in the cell and text2 is what should'nt be found.

Example:
A1 contains text "E8811, example, superb"
When I write to B1 function =takeif("E88";"uper";A1) result is blank. If "E88" is found but not "uper" then result is A1.

I have now:
Function takeif(txtyes As String, txtno As String, r As Range) As Boolean
takeif = InStr(r.Value, txtyes) > 0 And Not InStr(r.Value, txtno) > 0
End Function

It works ok but it returns FALSE or TRUE. It should return value of the A1 or blank.


EDIT. solved

Code:
<code>Function TakeIf(sMust As String, sMustNot As String, rRng As Range) As String Dim bRqd As Boolean bRqd = False If InStr(1, rRng.Value, sMust, vbTextCompare) > 0 Then bRqd = True If InStr(1, rRng.Value, sMustNot, vbTextCompare) > 0 Then bRqd = False If bRqd Then     TakeIf = rRng.Value Else     TakeIf = "" End If  End Function</code></pre>
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try This :

Function takeif(txtyes As String, txtno As String, r As Range) As Boolean
Select Case InStr(r.Value, txtyes) > 0 And Not InStr(r.Value, txtno) > 0
Case True
takeif = r.Value
Case False
takeif = ""
End Select
End Function
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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