How to check if a string is matching 2 criteria from 2 column

Ahmed Moursi 0 Reputation points
2024-02-04T08:47:23.8333333+00:00

Hello all, I need to check if content of a string is matching 2 columns in excel. Example what to type in cell B2, B3, B4, etc., to check the occurrence of cyproheptadine-levetiracetam or levetiracetam-cyproheptadine combination in cell A2, A3, A4, etc.. User's image Your help is highly appreciated. Thank you, Ahmed

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. Ahmed Moursi 0 Reputation points
    2025-08-04T09:07:30.7433333+00:00

    Hello All, thank you for your help, I've managed it with VBA, as below:

    Code in a module:

    Function FindInteractions(medString As String, medsA As Range, medsB As Range) As String

    Dim result As String

    Dim i As Long

    Dim medList As Variant

    Dim medSet As Object, pairSet As Object

    Set medSet = CreateObject("Scripting.Dictionary")

    Set pairSet = CreateObject("Scripting.Dictionary")

    ' Step 1: Parse A string into lowercase, trimmed, unique medication list

    medList = Split(LCase(medString), "-")

    For i = LBound(medList) To UBound(medList)

    Dim med As String

    med = Trim(medList(i))

    If Len(med) > 0 Then medSet(med) = True

    Next i

    ' Step 2: Only check to the last used row of medsA and medsB (optimization)

    Dim lastRow As Long

    lastRow = Application.WorksheetFunction.Max( _

    medsA.Worksheet.Cells(medsA.Worksheet.Rows.Count, medsA.Column).End(xlUp).Row, _

    medsB.Worksheet.Cells(medsB.Worksheet.Rows.Count, medsB.Column).End(xlUp).Row)

    ' Step 3: Loop through interaction table

    For i = 2 To lastRow ' assumes headers in row 1

    Dim medA As String, medB As String

    medA = LCase(Trim(medsA.Cells(i, 1).Value))

    medB = LCase(Trim(medsB.Cells(i, 1).Value))

    If Len(medA) > 0 And Len(medB) > 0 Then

    If medSet.exists(medA) And medSet.exists(medB) Then

    Dim combo As String

    If medA < medB Then

    combo = medA & "-" & medB

    Else

    combo = medB & "-" & medA

    End If

    If Not pairSet.exists(combo) Then pairSet(combo) = True

    End If

    End If

    Next i

    ' Step 4: Build result string from unique interaction pairs

    Dim key As Variant

    For Each key In pairSet.Keys

    If result <> "" Then result = result & "; "

    result = result & key

    Next key

    FindInteractions = result

    End Function

    and the formula in the cell:

    =FindInteractions(A2, B:B, C:C)

    This will test any possible occurrence and list multiple instances in front of each cell in column A.

    Many thanks to ChatGPT :) as well.

    Untitled

    0 comments No comments

  2. peiye zhu 165 Reputation points
    2025-08-04T11:40:34.04+00:00

    select * from Sheet1;

    create temp table aa as

    select f01 from Sheet1 f01;

    create temp table bb as

    select f03,f04 from Sheet1 f01 where f03<>'';

    select f01, group_concat(f03||'-'||f04,';') from aa left join bb on instr(f01,f03)>0 and instr(f01,f04)>0 group by f01;

    Screenshot_2025-08-04-19-39-47-751_com.mmbox.xbrowser.pro

    0 comments No comments

  3. Eleuterio Tedeschi 17,870 Reputation points Volunteer Moderator
    2025-08-05T16:52:17.83+00:00

    In this layout:

    User's image

    try

    B2
    =OR(REGEXTEST(A2;"."&$C$2:.$C$100&"."&$D$2:.$D$100&".*"))

    to drag, or using BYROW to expand them:
    =BYROW(A2:.A2000;LAMBDA(r;OR(REGEXTEST(r;"."&C2:.C100&"."&D2:.D100&".*"))))

    return true if combination in C and D is contained in A without udf in VBA or ChatGPT.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.