If a cell contains a string, return a value from an array

topi1

Active Member
Joined
Aug 6, 2014
Messages
252
Office Version
  1. 2010
In the following great formula/example from @Eric W, is it possible to change the formula in B, where it will give results even if not separated by whitespace and delimiters. It will help if it at least ignores delimiters like ".", "?" and "!". Thank you.

Book1
ABC
27SampleOutputKey
28Red wine on the carpetRedRed
29Blue -like the color or the moodBlueBlue
30yellow -testing if capitalization mattersYellowYellow
31xxgreenxx if the string needs to be isolated by whitespace or delimitersno matchGreen
32RedRed
33BlueBlue
34GreenGreen
35YellowYellow
Sheet1
Cell Formulas
RangeFormula
B28:B35B28=IFERROR(LOOKUP(2,1/SEARCH(" "&$C$28:$C$35&" "," "&A2&" "),$C$28:$C$35),"no match")
 
For the udf to return only the first word found left to right in col A, remove this line or change it from True to False.
VBA Code:
RX.Global = True
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
@Peter_SSs Made it work. Reviewing. B looks promising. It looks like it reads A from left to right. Need to strip the first word out. Will work on it and follow up. ty.
@Peter_SSs and @DanteAmor, you guys are expert at this and you would laugh at the following, but I converted B to H going step wise from E to H. Collective formula in H.

As a final result

i) it always gives the first key found in the string regardless of its position in C.
ii) Gives when there is not match. I may change that at some point.
iii) Removes ,./?!-_

Thank you guys.
Can't wait to modify Peter's vba and retry but meanwhile wanted to share this crazy derivation.
Thank you for listening.

Book2
ABCDEFGH
27SampleOutputKeyRemoves "no match""Strips first word""Removes ,./?-_"FINAL
28Red wine on the blue carpetRed, blueRedRed, blueRed,RedRed
29Blue -like the color or the moodBlueBlueBlueBlueBlueBlue
30yellow -testing if capitalization matters or redyellow, redYellowyellow, redyellow,yellowyellow
31xx green! if the string needs to be isolated by whitespace or delimitersgreenGreengreengreengreengreen
32Red? & blueRed, blueRed, blueRed,RedRed
33Blue & redBlue, redBlue, redBlue,BlueBlue
34GreenGreenGreenGreenGreenGreen
35yellowyellowyellowyellowyellowyellow
36Yellow.YellowYellowYellowYellowYellow
37Tom is unpredicatbleno match    
38Blue!, green & Red has several keysBlue, green, RedBlue, green, RedBlue,BlueBlue
Sheet1
Cell Formulas
RangeFormula
E28:E38E28=IF((CheckKeys(A28,C$28:C$31)="no match"),"",CheckKeys(A28,C$28:C$31))
F28:F38F28=IFERROR(LEFT(E28, FIND(" ", E28)-1), E28)
G28:G38G28=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(F28,",",""),"-",""),".",""),"!",""),"?",""),"_",""),"/","")
H28:H38H28=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(IFERROR(LEFT(IF((CheckKeys(A28,C$28:C$31)="no match"),"",CheckKeys(A28,C$28:C$31)), FIND(" ", IF((CheckKeys(A28,C$28:C$31)="no match"),"",CheckKeys(A28,C$28:C$31)))-1), IF((CheckKeys(A28,C$28:C$31)="no match"),"",CheckKeys(A28,C$28:C$31))),",",""),"-",""),".",""),"!",""),"?",""),"_",""),"/","")
B28:B38B28=CheckKeys(A28,C$28:C$31)
 
Upvote 0
For the udf to return only the first word found left to right in col A, remove this line or change it from True to False.
VBA Code:
RX.Global = True
@Peter_SSs changing from true to false changed the column B to what I had derived to H (not withstanding no match). Fantastic, Sir! Thank you. Why did I add those calculations? :giggle:

How hard or time consuming will it be to convert K part of the formula to K:K instead of specific range. Can make sure that there are no intervening blanks but it will be nice to have a whole range lookup. I know I am being greedy. Thank you.
 
Upvote 0
If you only want the first word left to right and don't want "no match" then the udf is even simpler.

VBA Code:
Function CheckKeys(s As String, rKeys As Range, Optional bIgnoreCase As Boolean = True) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = bIgnoreCase
  RX.Pattern = "\b(" & Join(Application.Transpose(rKeys), "|") & ")\b"
  If RX.Test(s) Then CheckKeys = RX.Execute(s)(0)
End Function

topi1_2.xlsm
ABC
27SampleOutputKey
28Red wine on the blue carpetRedRed
29Blue -like the color or the moodBlueBlue
30yellow -testing if capitalization matters or redyellowYellow
31xx green! if the string needs to be isolated by whitespace or delimitersgreenGreen
32Red? & blueRed
33Blue & redBlue
34GreenGreen
35yellowyellow
36Yellow.Yellow
37Tom is unpredicatble 
38Blue!, green & Red has several keysBlue
Sheet4
Cell Formulas
RangeFormula
B28:B38B28=CheckKeys(A28,C$28:C$31)



to convert K part of the formula to K:K instead of specific range.
What is the "K part of the formula"?
In general I would recommend not using whole column References, but if you explain in detail &/or give examples of what you mean I can consider the question.
 
Upvote 0
If you only want the first word left to right and don't want "no match" then the udf is even simpler.

VBA Code:
Function CheckKeys(s As String, rKeys As Range, Optional bIgnoreCase As Boolean = True) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = bIgnoreCase
  RX.Pattern = "\b(" & Join(Application.Transpose(rKeys), "|") & ")\b"
  If RX.Test(s) Then CheckKeys = RX.Execute(s)(0)
End Function

topi1_2.xlsm
ABC
27SampleOutputKey
28Red wine on the blue carpetRedRed
29Blue -like the color or the moodBlueBlue
30yellow -testing if capitalization matters or redyellowYellow
31xx green! if the string needs to be isolated by whitespace or delimitersgreenGreen
32Red? & blueRed
33Blue & redBlue
34GreenGreen
35yellowyellow
36Yellow.Yellow
37Tom is unpredicatble 
38Blue!, green & Red has several keysBlue
Sheet4
Cell Formulas
RangeFormula
B28:B38B28=CheckKeys(A28,C$28:C$31)




What is the "K part of the formula"?
In general I would recommend not using whole column References, but if you explain in detail &/or give examples of what you mean I can consider the question.
Right now, the formula looks at C28:C31. It becomes tedious to manually change it if C28:C41 are not blank and need to be included in the lookup. I guess I would change to C28:C100 or something to make room for C column's growth. Thank you.
 
Upvote 0
Try this then. It is partly based on your approach in post #12 where you extracted just the first word. From that I have assumed that the values in the Key list will all be single words. If that is not the case then more clarification/examples would be needed. Note that I have used a different function name here since we are only extracting (at most) a single key.

VBA Code:
Function GetKey(s As String, rKeys As Range, Optional bIgnoreCase As Boolean = True) As String
  Dim RX As Object

  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = bIgnoreCase
  RX.Pattern = "\b(" & Replace(Application.Trim(Join(Application.Transpose(rKeys))), " ", "|") & ")\b"
  If RX.Test(s) Then GetKey = RX.Execute(s)(0)
End Function

topi1_2.xlsm
ABC
27SampleOutputKey
28Red wine on the blue carpetRed
29Blue -like the color or the moodBlue
30yellow -testing if capitalization matters or redyellowRed
31xx green! if the string needs to be isolated by whitespace or delimitersgreen
32Red? & blueRedBlue
33Blue & redBlue
34GreenGreenYellow
35yellowyellowGreen
36Yellow.Yellow
37Tom is unpredicatble 
38Blue!, green & Red has several keysBlue
Sheet4
Cell Formulas
RangeFormula
B28:B38B28=GetKey(A28,C$28:C$100)
 
Upvote 0
@Peter_SSs Brilliant! Thank you so much for your continued support and changing the script. There is a chance that I may use "no match" or "others" if the results are blank in the column B. I can clearly change the formula in the column B to accomplish that, but I am curious if the vba can be modified to say "no match'. I know I am going back some. Regardless, with the change of formula to vba, I have something I was hoping for. Thank you all of you for your help.
 
Upvote 0
Thank you so much for your continued support and changing the script.
You're welcome. :)

There is a chance that I may use "no match" or "others" if the results are blank ....
Add the extra line shown below and edit the text if you want to whatever result you want if it cannot find any of the Keys.

Rich (BB code):
Function GetKey(s As String, rKeys As Range, Optional bIgnoreCase As Boolean = True) As String
  Dim RX As Object

  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = bIgnoreCase
  RX.Pattern = "\b(" & Replace(Application.Trim(Join(Application.Transpose(rKeys))), " ", "|") & ")\b"
  GetKey = "no match"
  If RX.Test(s) Then GetKey = RX.Execute(s)(0)
End Function
 
Upvote 0
You're welcome. :)


Add the extra line shown below and edit the text if you want to whatever result you want if it cannot find any of the Keys.

Rich (BB code):
Function GetKey(s As String, rKeys As Range, Optional bIgnoreCase As Boolean = True) As String
  Dim RX As Object

  Set RX = CreateObject("VBScript.RegExp")
  RX.IgnoreCase = bIgnoreCase
  RX.Pattern = "\b(" & Replace(Application.Trim(Join(Application.Transpose(rKeys))), " ", "|") & ")\b"
  GetKey = "no match"
  If RX.Test(s) Then GetKey = RX.Execute(s)(0)
End Function
Genius! Worked like a charm! I'll change it to "" when I want it blank but now I know how to modify it.
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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