Cell contains several of many text strings then return multiple lookup values

MHamid

Active Member
Joined
Jan 31, 2013
Messages
472
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,
I am wondering if someone would know if this would be possible. I have two sheets in a workbook. One is my "Main" sheet and the other is "Key" sheet. In my "Main" sheet, I have a column where the cell value is a string with various codes with an without the parenthesis. I would like to search through that string based on a key search using a range column (column A) in the "Key" sheet and would like to bring back a result found in column D of the "Key" sheet. Would this be possible using a formula or VBA code. I would prefer a VBA code, but I would accept either at this point.
I cannot provide the actual data, but below is a what I am looking for. One of the issues I have is that I need to find the exact match. For instance, if I am looking for 12345. I expect to match 12345 with 12345 ... 12345 should not be match to 1234567.

"Main" sheet: Column A has the string cell to be searched through for matches and column B shows the results that I would like to see.
Managed Segment (Home)-Node NumberBusiness Name
Business 1 [L6](12345) - 12345;Business 2 [L7](45678) - 45678Business/Technology
Business 1 [L6](12345) - 12345;Business 2 [L7](45678) - 45678;Business 2a [L7](78945) - 78945Business/Technology/Operations
Business 2 [L7](96385) - 96385Business
Business 3 [L5] - 1234567Operations
Business 3 - 1234567;Business 4 - 45612Operations/Technology


"Key" Sheet: Houses the search/lookup values in column A and the result I want brought back is in Column D.
MS IDMS ID NameSectorBusiness Name
(12345)Business 1 [L6]sector1Business
(45678)Business 2 [L7]sector2Technology
(78945)Business 2a [L7]sector3Operations
(96385)Business 2 [L7]sector1Business
(1234567)Business 3sector3Operations
(45612)Business 4sector2Technology
12345Business 1 [L6]sector1Business
45678Business 2 [L7]sector2Technology
78945Business 2a [L7]sector3Operations
96385Business 2 [L7]sector1Business
1234567Business 3sector3Operations
45612Business 4sector2Technology


Let me know if more information is needed.
 
Give this one a try

VBA Code:
Sub BusinessNames_v2()
  Dim RX As Object, M As Object, d As Object, d2 As Object
  Dim a As Variant, b As Variant
  Dim i As Long
  Dim s As String, sM As String
  
  Set d = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  With Sheets("Key")
    RX.Pattern = "(\b)(" & Join(Application.Transpose(.Range("B2", .Range("B" & Rows.Count).End(xlUp)).Value), "|") & ")"
    RX.Pattern = Replace(Replace(RX.Pattern, "[", "\["), "]", "\]")
    a = .Range("B2", .Range("D" & Rows.Count).End(xlUp)).Value
  End With
  For i = 1 To UBound(a)
    d(CStr(a(i, 1))) = a(i, 3)
  Next i
  With Sheets("Main")
    a = .Range("G2", .Range("G" & Rows.Count).End(xlUp)).Value
    ReDim b(1 To UBound(a), 1 To 1)
    For i = 1 To UBound(a)
      d2.RemoveAll
      s = vbNullString
      For Each M In RX.Execute(a(i, 1))
        sM = M
        If Not d2.exists(d(sM)) Then
          s = s & "/" & d(sM)
          d2(d(sM)) = 1
        End If
      Next M
      b(i, 1) = Mid(s, 2)
    Next i
    .Range("AC2").Resize(UBound(b)).Value = b
  End With
End Sub
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi Peter,

I am getting the following error message when I run the code.
"Application-defined or object-defined error" and the below is highlighted in yellow in the code.

VBA Code:
 For Each M In RX.Execute(a(i, 1))


Thank you,
 
Upvote 0
I am getting the following error message when I run the code.
"Application-defined or object-defined error" and the below is highlighted in yellow in the code.

VBA Code:
 For Each M In RX.Execute(a(i, 1))


Thank you,
The code worked for me with the sample data provide in post #10.
  1. Have you tried the code with that same sample data?
  2. When you get the error and click Debug, what value shows in the pop-up when you hover over M in that yellow line?
  3. What value shows in the pop-up when you hover over i in that yellow line?
 
Upvote 0
Hi Peter,

  1. Have you tried the code with that same sample data?
I did try the code with the same sample data and it works fine.

  1. When you get the error and click Debug, what value shows in the pop-up when you hover over M in that yellow line?
When I hover over it, it states M=Nothing

What value shows in the pop-up when you hover over i in that yellow line?
it states i = 1

This is what I see in the Locals window:
1671208559934.png



When I copied the data to my sheet, the only changes I made to it was in the Main Sheet section. I updated G2 to G3 and then updated AC2 to AC3 because that's where my data starts. I made the same changes in the sample data and it worked fine in the sample data, but for some reason it's still not working with my actual data.
Can you explain how the macro works? Maybe it's something I'm missing with the actual data.


Thank you,
 
Upvote 0
Hi Peter,


I did try the code with the same sample data and it works fine.


When I hover over it, it states M=Nothing


it states i = 1

This is what I see in the Locals window:
View attachment 81057


When I copied the data to my sheet, the only changes I made to it was in the Main Sheet section. I updated G2 to G3 and then updated AC2 to AC3 because that's where my data starts. I made the same changes in the sample data and it worked fine in the sample data, but for some reason it's still not working with my actual data.
Can you explain how the macro works? Maybe it's something I'm missing with the actual data.


Thank you,
Not sure if this might help, but the data I have in cell G3 of the Main sheet is like this:
Global Markets Treasury [L6] 26395 -- Fixed Income Currencies and Commodities [L5] 29414 -- Global Markets [L4] 5 -- Institutional Clients Group [L3] 3 -- Core Citi Businesses [L2] 20448 -- Total Citi [L1] 1;International Treasury [L7] 22440 -- Treasury [L6] 9905121 -- Finance [L5] 5000 -- Global Functions and Other Corporate Activities [L4] 4923 -- Enterprise O&T Global Functions & Other Corporate [L3] 25302 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1

Would that make a difference in your code? I ask because the error seems to be stating that the object doe snot exist, but there is data in cell G3.
 
Upvote 0
Sorry, I also just added the expressions for that line in the watchlist and received the below results. Not sure exactly how to fix.

1671214959491.png
 
Upvote 0
If it works for the sample data but not for your real data then it is almost certainly related to your data and how it differs from the sample.
My suspicion is the inclusion in ID Names of other special characters that are used in regular expression syntax which need modification as I did with the "[" and "]" symbols in this line
VBA Code:
RX.Pattern = Replace(Replace(RX.Pattern, "[", "\["), "]", "\]")
Another possibility though is it could relate to any changes you have made to the code.

the data I have in cell G3 of the Main sheet is like this:
Global Markets Treasury [L6] 26395 -- Fixed Income Currencies and Commodities [L5] 29414 -- Global Markets [L4] 5 -- Institutional Clients Group [L3] 3 -- Core Citi Businesses [L2] 20448 -- Total Citi [L1] 1;International Treasury [L7] 22440 -- Treasury [L6] 9905121 -- Finance [L5] 5000 -- Global Functions and Other Corporate Activities [L4] 4923 -- Enterprise O&T Global Functions & Other Corporate [L3] 25302 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1
So can you post (with XL2BB) the range G1:G4 from 'Main' and columns B:D (but hide column C) from 'Key' and then also post the modified code?
 
Upvote 0
Hi Peter,

Here is the updated data.
Testing.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
1Account NumberBL1BL2BL3BL4MS - Node NumberMS - HomeImpct Node numberImpact HomeBusiness 1Business 2Business 3Business 4Business 5Business 6Business 7Business 8Business 9Business 10Business 11Business 12Business 13Business 14Business 15Business 16Business 17Business 18AreaBusiness Name
2S123456GBBusiness 1Business 1Business 1Business 1 [L6](12345) - 12345;Business 2 [L7](45678) - 45678Global Markets Treasury [L6] 26395 -- Fixed Income Currencies and Commodities [L5] 29414 -- Global Markets [L4] 5 -- Institutional Clients Group [L3] 3 -- Core Citi Businesses [L2] 20448 -- Total Citi [L1] 1;OOBC [L7] -- ER [L6] -- COO [L5] -- EO&T [L4] -- EiO&T GF & OC [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
3S456789ETBusiness 2Business 2Business 2Business 1 [L6](12345) - 12345;Business 2 [L7](45678) - 45678;Business 6 [L5](78945) - 78945Global Markets Treasury [L6] 26395 -- Fixed Income Currencies and Commodities [L5] 29414 -- Global Markets [L4] 5 -- Institutional Clients Group [L3] 3 -- Core Citi Businesses [L2] 20448 -- Total Citi [L1] 1;OOBC [L7] -- ER [L6] -- COO [L5] -- EO&T [L4] -- EiO&T GF & OC [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1; FRSS Mng [L7] 278003 -- Finanac eand Risk Shared Services [L6] 9908260 -- Total Citi Shared Services [L5] 8163 -- Enterprise Operations & Technology[L4] 5497 -- -- Enterprise O&T Global Functions & Other Corporate [L3] 25302 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
4S789123IGBusiness 3Business 3Business 3Business 5 [L7](96385) - 96385Treasury [L6] -- Finance [L5] -- Global Markets [L4] -- EIO&T [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
5S456123PBBusiness 4Business 4Business 4Business 3 [L5] - 1234567International Treasury [L7] 22440 -- Treasury [L6] 9905121 -- Finance [L5] 5000 -- Global Functions and Other Corporate Activities [L4] 4923 -- Enterprise O&T Global Functions & Other Corporate [L3] 25302 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1Liquidity [L7] 9921639 -- TTS CO [L6] 4349 -- DEF Ops [L5] 22939 -- DEF O&T [L4] 23078 -- DEF [L3] 3 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
6S852741PBBusiness 4Business 4Business 4
7S741963ETBusiness 5Business 5Business 5Business 3 [L5]- 1234567;Business 4 [L6]- 45612OOBC [L7] -- ER [L6] -- COO [L5] -- EO&T [L4] -- EiO&T GF & OC [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1;Liquidity [L7] 9921639 -- TTS CO [L6] 4349 -- ABC Ops [L5] 22939 -- ABC O&T [L4] 23078 -- ABC [L3] 3 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
8S741852GBBusiness 5Business 5Business 5siness 2 [L7](45678) - 45678;Business 5 [L7](96385) - 96385;Business 3 [L5]- 1234567;Business 4 [L6]- 45612International Treasury [L7] 22440 -- Treasury [L6] 9905121 -- Finance [L5] 5000 -- Global Functions and Other Corporate Activities [L4] 4923 -- Enterprise O&T Global Functions & Other Corporate [L3] 25302 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1;Treasury [L6] -- Fiinance [L5] -- Global Markets [L4] -- EIO&T [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1;OOBC [L7] -- ER [L6] -- COO [L5] -- EO&T [L4] -- EiO&T GF & OC [L3] -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1;Liquidity [L7] 9921639 -- TTS CO [L6] 4349 -- ABC Ops [L5] 22939 -- ABC O&T [L4] 23078 -- ABC [L3] 3 -- Corporate / Other [L2] 4921 -- Total Citi [L1] 1other dataother data
Main


Testing.xlsm
ABD
1ID #ID NameBusiness Name
212345Global Markets Treasury [L6]Business
345678Total Citi Shared Services [L5]Technology
478945ABC Ops [L5]Operations
596385Finance [L5]Business
61234567DEF Ops [L5]Operations
745612COO [L5]Technology
Key



i'm still working on the pattern to see if that is the issue. Also, the only thing I updated in the code was just the range in the main sheet from G2 to G3 and AC2 to AC3. I didn't change anything else in the code.

Let me know if you are getting an error with the updated data I provided.



Thank you,
 
Upvote 0
Let me know if you are getting an error with the updated data I provided.
I don't get any error if I use that sample data in the two sheets.
Have you tried it with that sample data only?

How many rows do you actually have in the 'Key' sheet? If it is not too many (& the data is not sensitive) can you post that? I suspect any problem will lie with one or more of those actual values.

BTW, when posting XL2BB sample data, only post the relevant columns. For example, with Main you have post 29 columns when all we really needed was column G.
If you did want to show, say column G and column AC, then hide all the columns between them before creating your mini sheet.
 
Upvote 0
I don't get any error if I use that sample data in the two sheets.
Have you tried it with that sample data only?

How many rows do you actually have in the 'Key' sheet? If it is not too many (& the data is not sensitive) can you post that? I suspect any problem will lie with one or more of those actual values.

BTW, when posting XL2BB sample data, only post the relevant columns. For example, with Main you have post 29 columns when all we really needed was column G.
If you did want to show, say column G and column AC, then hide all the columns between them before creating your mini sheet.

I tried it with the sample data and I didn't get an error.

In the 'Key' sheet, there are currently 5605 rows of data. I was going through the process of elimination to see at what point the code will stop working and found what was causing the error. There was one data point listed as BusinessName( [L1]. The business name was listed with just one parentheses without a closure. Is there any way to avoid something like this causing an error. Could we add something within the code to state that if there is only 1 instance of a parentheses to remove it without affecting any other data that has the open and close parentheses?

My apologies for the data I provided with the XL2BB. I thought I did hide those columns. I will ensure to provide relevant data.


Thank you,
 
Upvote 0

Forum statistics

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