
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.