Textjoin with wildcard (Search,Unique)

hwong8848

New Member
Joined
Oct 9, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hello all!

I have a question regarding looking up similar companies.

For example, in my database, there is AXA Asia, AXA Group or AXA Company Limited. I will look up AXA with search function and filter them out.

My formula is like this

=IFERROR(UNIQUE(FILTER(Uni_Comp_list,ISNUMBER(SEARCH(A2,Uni_Comp_list)))),"N/A")

where A2 is AXA and Uni_Comp_list contain all the different company name.

However the formula will spill since there are more than one match, and I will have to copy and paste it everytime.

I tried using textjoin(",",,True,A2:A16) coupled with the formula above but have no luck.

Any help is appreciated.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
No


  1. Perhaps you could ask them to update it?
  2. Failing that, will corporate allow you to use vba to solve the problem?
They are quite reluctant to keeping office suite up to date.
VBA is definitely an option! Will you let me know how can I do it? :)
 
Upvote 0
VBA is definitely an option!
Remove any formulas from column B and try this code.

VBA Code:
Sub List_Companies()
  Dim d As Object
  Dim a As Variant, b As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long
  Dim Co As String
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("Uni_Comp_List")
  ReDim Results(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  b = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For j = 1 To UBound(b)
    For i = d.Count To 1 Step -1
      Co = d.keys()(i - 1)
      If InStr(1, Co, b(j, 1), 1) > 0 Then
          k = k + 1
          Results(k, 1) = Co
          d.Remove Co
      End If
    Next i
  Next j
  With Range("B2").Resize(k)
    .Value = Results
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
  End With
End Sub
 
Upvote 0
Remove any formulas from column B and try this code.

VBA Code:
Sub List_Companies()
  Dim d As Object
  Dim a As Variant, b As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long
  Dim Co As String
 
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("Uni_Comp_List")
  ReDim Results(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  b = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For j = 1 To UBound(b)
    For i = d.Count To 1 Step -1
      Co = d.keys()(i - 1)
      If InStr(1, Co, b(j, 1), 1) > 0 Then
          k = k + 1
          Results(k, 1) = Co
          d.Remove Co
      End If
    Next i
  Next j
  With Range("B2").Resize(k)
    .Value = Results
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
  End With
End Sub
Thank you so much! Do you mind explaining a bit with me? As I have 2 more lists to go through and hope I can edit myself rather than bugging this forum.

Template.xlsm
ABCDEF
1Peer Group 1Current Output and setupPeer Group 2Wildcard lookup Peer Group 3Wildcard lookup
2AIAAIAAIAAegon
3China LifeChina LifeAllianzAgeas
4China PacificChina Life (Taiwan)AXAAflac
5China Tai pingChina PacificChina Tai pingAIG
6DBSChina Tai PingGreat EasternAviva
7FWDDBSFWDBarclay
8Great EasternFWDGeneraliBank of America
9Hang Seng Great EasternHSBCBNP Paribas
10ManulifeHang Seng ManulifeChubb
11MetlifeManulifeMetlifeCigna
12New China LifeManulife FinancialPing AnCiti
13Ping AnManulife Financial (Canada)Prudential FinancialDBS
14Standard CharteredMetLifePCAFTLife
15United OverseasMetLife, Inc.Sun LifeHartford
16UOBPing AnZurich InsuranceHSBC
17Standard CharteredING Bank
18Standard Chartered BankLegal & General
19UOBMorgan Stanley
20Royal Bank of Canada
21RBC
22Swiss
23Traveler
24
25
26
27
Peer Group
 
Upvote 0
Do you mind explaining a bit with me?
Sure. I have altered the order of the code slightly to aid descriptions.

VBA Code:
Sub List_Companies()
  Dim d As Object
  Dim a As Variant, b As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long
  Dim Co As String
  
  'Create a dictionary (just a list in this case) of all the companies in Uni_Comp_List
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("Uni_Comp_List")
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  
  'Set up an array big enough just in case all Uni_Comp_List companies need to be returned
  ReDim Results(1 To UBound(a), 1 To 1)
  
  'Put the Peer Group 1 values into an array (for faster processing)
  b = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  
  'For each Peer Group value
  For j = 1 To UBound(b)
    
    'Work through the dictionary list
    For i = d.Count To 1 Step -1
      'Get the company name from the dictionary
      Co = d.keys()(i - 1)
      
      'If the Peer Group value is found in the company name then
      If InStr(1, Co, b(j, 1), 1) > 0 Then
          'Put that company name into the next row of the results array ..
          k = k + 1
          Results(k, 1) = Co
          '.. & remove that company from the dictionary as it does not need checking again
          'This will speed the future processing a little
          d.Remove Co
      End If
    Next i
  Next j
  
  'Put the results into the worksheet & sort them
  With Range("B2").Resize(k)
    .Value = Results
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
  End With
End Sub
 
Upvote 0
VBA Code:
Sub List_Companies()
' This macro extract similar companies from uni_Comp_list
  Dim d As Object
  Dim a As Variant, pg1 As Variant, pg2 As Variant, pg3 As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long
  Dim Co As String
  
  'Create a dictionary (just a list in this case) of all the companies in Uni_Comp_List
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("Uni_Comp_List")
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  
  'Set up an array big enough just in case all Uni_Comp_List companies need to be returned
  ReDim Results(1 To UBound(a), 1 To 1)
  
  'Put the Peer Group values into an array (for faster processing)
  pg1 = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  pg2 = Range("C2", Range("c" & Rows.Count).End(xlUp)).Value
  pg3 = Range("E2", Range("E" & Rows.Count).End(xlUp)).Value
  
  'For each Peer Group value
  For j = 1 To UBound(pg1)
    
    'Work through the dictionary list
    For i = d.Count To 1 Step -1
      'Get the company name from the dictionary
      Co = d.keys()(i - 1)
      
      'If the Peer Group value is found in the company name then
      If InStr(1, Co, pg1(j, 1), 1) > 0 Then
          'Put that company name into the next row of the results array ..
          k = k + 1
          Results(k, 1) = Co
          '.. & remove that company from the dictionary as it does not need checking again
          'This will speed the future processing a little
          d.Remove Co
      End If
    Next i
  Next j
  
  'Put the results into the worksheet & sort them
  With Range("B2").Resize(k)
    .Value = Results
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
  End With
End Sub

I have tried and compile PG1 to PG3 as an array, but after that I am quite lost on where to edit to process these lists, a dumb way is to copy the pg1 "Ifs statement", however is there a way to do this as a loop? (Just in case there are more than 3 lists in the future)
 
Upvote 0
If I have understood correctly, this is one way.

VBA Code:
Sub List_Companies_v2()
  Dim d As Object
  Dim a As Variant, b As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long, PG As Long
  Dim Co As String
  
  'Create a dictionary ready for all the companies in Uni_Comp_List
  Set d = CreateObject("Scripting.Dictionary")
  'Get the Uni_Comp_List into an array
  a = Range("Uni_Comp_List")

  'For each Peer Group
  For PG = 1 To 3
  
    'Clear dictionary & reload
    d.RemoveAll
    For i = 1 To UBound(a)
      d(a(i, 1)) = 1
    Next i
    
    'Set up an array big enough in case all Uni_Comp_List companies need to be returned
    ReDim Results(1 To UBound(a), 1 To 1)
    
    'Reset k
    k = 0
    
    'Put the Peer Group values into an array (PG * 2 - 1 will give columns A, C & E)
    b = Range(Cells(2, PG * 2 - 1), Cells(Rows.Count, PG * 2 - 1).End(xlUp)).Value
    
    'For each Peer Group value
    For j = 1 To UBound(b)
      
      'Work through the dictionary list
      For i = d.Count To 1 Step -1
        'Get the company name from the dictionary
        Co = d.keys()(i - 1)
        
        'If the Peer Group value is found in the company name then
        If InStr(1, Co, b(j, 1), 1) > 0 Then
            'Put that company name into the next row of the results array ..
            k = k + 1
            Results(k, 1) = Co
            '.. & remove that company from the dictionary as it does not need checking again
            'This will speed the future processing a little
            d.Remove Co
        End If
      Next i
    Next j
    
    'Put results into the worksheet & sort (PG * 2 will give columns B, D & F)
    With Cells(2, 2 * PG).Resize(k)
      .Value = Results
      .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
    End With
  Next PG
End Sub

Here are my results based on the previous Uni_Comp_List (which I now have in column I but not shown here)

hwong8848.xlsm
ABCDEF
1Peer Group 1Current Output and setupPeer Group 2Wildcard lookup Peer Group 3Wildcard lookup
2AIAAIAAIAAIAAegonAegon
3China LifeChina LifeAllianzAllianzAgeasAEGON Group
4China PacificChina Life (Taiwan)AXAAllianz Life InsuranceAflacAegon N.V.
5China Tai pingChina PacificChina Tai pingAllianz SEAIGAFLAC
6DBSChina Tai PingGreat EasternAllianz Worldwide PartnersAvivaAflac Incorporated
7FWDDBSFWDAssicurazioni Generali S.p.A.BarclayAgeas
8Great EasternFWDGeneraliAXABank of AmericaAIG
9Hang Seng Great EasternHSBCAXA GroupBNP ParibasAviva
10ManulifeHang Seng ManulifeChina Tai PingChubbBarclays
11MetlifeManulifeMetlifeFWDCignaBNP Paribas
12New China LifeManulife FinancialPing AnGeneraliCitiChubb
13Ping AnManulife Financial (Canada)Prudential FinancialGreat EasternDBSChubb Life
14Standard CharteredMetLifePCAHSBCFTLifeCigna
15United OverseasMetLife, Inc.Sun LifeHSBC BankHartfordCitigroup
16UOBPing AnZurich InsuranceManulifeHSBCDBS
17Standard CharteredManulife FinancialING BankFTLife
18Standard Chartered BankManulife Financial (Canada)Legal & GeneralHartford Financial Services Group
19UOBMetLifeMorgan StanleyHSBC
20MetLife, Inc.Royal Bank of CanadaHSBC Bank
21PCARBCING Bank
22Ping AnSwissLegal & General America
23Prudential FinancialTravelerLegal & General Group
24Prudential Financial, Inc.Morgan Stanley
25Sun LifeSwiss Re
26Sun Life FinancialSwiss Reinsurance
27Sun Life Financial Inc.The Hartford Financial Services Group, Inc.
28Zurich Insurance GroupTravelers
29Zurich Insurance Group AG
Sheet3
 
Upvote 0
Solution
Try this in K2
Excel Formula:
=LET(a,FILTER(B2:B25,B2:B25<>""),b,FILTER(G2:G95,G2:G95<>""),c,IF(TRANSPOSE(MMULT(1*(TRANSPOSE(a)>0),1*ISNUMBER(SEARCH(a,TRANSPOSE(b)))))>0,b,""),d,FILTER(c,c<>""),SORT(d))


Peer Group 1Current Output and setupDesired OutputUni_Comp_listFORMULA
2AIAAIAAIAAegonAIA
3China LifeChina LifeChina Life (Taiwan)AgeasChina Life
4China Life (Taiwan)China LifeAXAChina Life (Taiwan)
5China PacificChina PacificChina PacificAIAChina Pacific
6China Tai pingChina Tai PingTRUEChina Tai PingBNPP CardifChina Tai Ping
7DBSDBSDBSAonDBS
8FWDFWDFWDGeneraliFWD
9Great EasternGreat EasternGreat EasternChubbGreat Eastern
10Hang SengHang SengHang SengChubb LifeHang Seng
11ManulifeManulifeManulifeCignaManulife
12Manulife FinancialManulife Financial (Canada)FWDManulife Financial
13Manulife Financial (Canada)Manulife FinancialGreat EasternManulife Financial (Canada)
14MetlifeMetLifeMetLifeHannover ReMetLife
15MetLife, Inc.MetLife, Inc.LibertyMetLife, Inc.
16New China LifePing AnN/AManulifePing An
17Ping AnStandard CharteredPing AnMetLifeStandard Chartered
18Standard CharteredStandard Chartered BankStandard Chartered BankMunichReStandard Chartered Bank
19UOBStandard CharteredPeak ReUOB
20United OverseasN/APCA
21UOBUOBPrudential Financial
22QBE
23RGA
24Sun Life
25Swiss Re
26Zurich
27Sompo
28Allianz
29SCOR
30Tokio Marine
31MSIG
32Huatai
33Marsh
34Ping An
35China Pacific
36Cathay
37AIG
38PartnerRe
39Fubon
40FTLife
41HSBC
42Transatlantic
43MCIS
44Etiqa
45General Re
46Muang Thai
47Ocean Life
48NanShan
49TransGlobe
50Shin Kong
51China Life (Taiwan)
52AFLAC
53Travelers
54Hartford Financial Services Group
55Allianz Life Insurance
56Citigroup
57Manulife Financial (Canada)
58Manulife Financial
59Legal & General America
60HSBC Bank
61Sun Life Financial
62Zurich North America
63Barclays
64Aflac Incorporated
65MetLife, Inc.
66Morgan Stanley
67The Hartford Financial Services Group, Inc.
68Allianz SE
69Standard Chartered Bank
70Aviva
71BNP Paribas
72AXA Group
73Allianz Worldwide Partners
74AEGON Group
75ING Bank
76Zurich Insurance Group
77Swiss Reinsurance
78Legal & General Group
79Sun Life Financial Inc.
80American International Group, Inc.
81Prudential Financial, Inc.
82Aegon N.V.
83Assicurazioni Generali S.p.A.
84Zurich Insurance Group AG
85China Tai Ping
86PingAn
87China Life
88Standard Chartered
89UOB
90Hang Seng
91DBS
 
Upvote 0
V
If I have understood correctly, this is one way.

VBA Code:
Sub List_Companies_v2()
  Dim d As Object
  Dim a As Variant, b As Variant, Results As Variant
  Dim i As Long, j As Long, k As Long, PG As Long
  Dim Co As String
 
  'Create a dictionary ready for all the companies in Uni_Comp_List
  Set d = CreateObject("Scripting.Dictionary")
  'Get the Uni_Comp_List into an array
  a = Range("Uni_Comp_List")

  'For each Peer Group
  For PG = 1 To 3
 
    'Clear dictionary & reload
    d.RemoveAll
    For i = 1 To UBound(a)
      d(a(i, 1)) = 1
    Next i
   
    'Set up an array big enough in case all Uni_Comp_List companies need to be returned
    ReDim Results(1 To UBound(a), 1 To 1)
   
    'Reset k
    k = 0
   
    'Put the Peer Group values into an array (PG * 2 - 1 will give columns A, C & E)
    b = Range(Cells(2, PG * 2 - 1), Cells(Rows.Count, PG * 2 - 1).End(xlUp)).Value
   
    'For each Peer Group value
    For j = 1 To UBound(b)
     
      'Work through the dictionary list
      For i = d.Count To 1 Step -1
        'Get the company name from the dictionary
        Co = d.keys()(i - 1)
       
        'If the Peer Group value is found in the company name then
        If InStr(1, Co, b(j, 1), 1) > 0 Then
            'Put that company name into the next row of the results array ..
            k = k + 1
            Results(k, 1) = Co
            '.. & remove that company from the dictionary as it does not need checking again
            'This will speed the future processing a little
            d.Remove Co
        End If
      Next i
    Next j
   
    'Put results into the worksheet & sort (PG * 2 will give columns B, D & F)
    With Cells(2, 2 * PG).Resize(k)
      .Value = Results
      .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlNo
    End With
  Next PG
End Sub

Here are my results based on the previous Uni_Comp_List (which I now have in column I but not shown here)

hwong8848.xlsm
ABCDEF
1Peer Group 1Current Output and setupPeer Group 2Wildcard lookup Peer Group 3Wildcard lookup
2AIAAIAAIAAIAAegonAegon
3China LifeChina LifeAllianzAllianzAgeasAEGON Group
4China PacificChina Life (Taiwan)AXAAllianz Life InsuranceAflacAegon N.V.
5China Tai pingChina PacificChina Tai pingAllianz SEAIGAFLAC
6DBSChina Tai PingGreat EasternAllianz Worldwide PartnersAvivaAflac Incorporated
7FWDDBSFWDAssicurazioni Generali S.p.A.BarclayAgeas
8Great EasternFWDGeneraliAXABank of AmericaAIG
9Hang Seng Great EasternHSBCAXA GroupBNP ParibasAviva
10ManulifeHang Seng ManulifeChina Tai PingChubbBarclays
11MetlifeManulifeMetlifeFWDCignaBNP Paribas
12New China LifeManulife FinancialPing AnGeneraliCitiChubb
13Ping AnManulife Financial (Canada)Prudential FinancialGreat EasternDBSChubb Life
14Standard CharteredMetLifePCAHSBCFTLifeCigna
15United OverseasMetLife, Inc.Sun LifeHSBC BankHartfordCitigroup
16UOBPing AnZurich InsuranceManulifeHSBCDBS
17Standard CharteredManulife FinancialING BankFTLife
18Standard Chartered BankManulife Financial (Canada)Legal & GeneralHartford Financial Services Group
19UOBMetLifeMorgan StanleyHSBC
20MetLife, Inc.Royal Bank of CanadaHSBC Bank
21PCARBCING Bank
22Ping AnSwissLegal & General America
23Prudential FinancialTravelerLegal & General Group
24Prudential Financial, Inc.Morgan Stanley
25Sun LifeSwiss Re
26Sun Life FinancialSwiss Reinsurance
27Sun Life Financial Inc.The Hartford Financial Services Group, Inc.
28Zurich Insurance GroupTravelers
29Zurich Insurance Group AG
Sheet3
Thank you very much, works like a charm!
 
Upvote 0
Cheers. Glad it worked for you. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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