You are here:

Excel/Extract variable entangled content


Dear Tom, first off: a very happy new year to you!

Using Excel 2010, I've got alphanumeric data in cells A2 to B... (usually 200+ rows).
With formulas from C2 and D2 copying down, or VBA, how could I extract each Acnt (cnt for alphanumeric content) that relates to the B content in column C, and each Bcnt that relates to the A content in column D, conform below sample?

In the below sample, cells C5 and D5 equal the content of cells A5 and B5 respectively because content 'A1' only links to content 'B1', and 'B1' links to 'A1' alone. One or more contents of column A could relate to those of column B and the other way around. Hence cell C2 lists A5&A6&A7 as the 'A6' content in cell A2 links to content 'B6' in cell B2, whilst 'B6' also relates to content 'A7' (cell A7), that in turn links to content 'B5' (cell B4), where 'B5' links to 'A5' as well (cell A9).
  A   B   C        D
1   Acnt   Bcnt   Aextract     Bextract
2   A6   B6   A5&A6&A7  B5&B6
3   A4   B3   A4         B3&B4
4   A7   B5   A5&A6&A7  B5&B6
5   A1   B1   A1         B1
6   A3   B2   A2&A3      B2
7   A7   B6   A5&A6&A7   B5&B6
8   A2   B2   A2&A3       B2
9   A5   B5   A5&A6&A7   B5&B6
10   A4   B4   A4          B3&B4

Many thanks in advance, kind regards, Joe


OK, I had some time to play with it and came up with this macro. It reproduces the results you show (although the string of characters in columns C and D are not in the same order you show).  

Sub abc()
Dim vA() As String, vB() As String
Dim r As Range, cell As Range, cell1 As Range
Dim i As Long, j As Long
Dim sA As String, sB As String
Dim added As Boolean, bFound As Boolean
Set r = Range("A2", Cells(Rows.Count, 1).End(xlUp))
For Each cell In r
 ReDim vA(1 To 1)
 ReDim vB(1 To 1)
 vA(1) = cell.Value
 vB(1) = cell.Offset(0, 1).Value
  added = False
  For Each cell1 In r
   If cell1.Address <> cell.Address Then
     For i = LBound(vA) To UBound(vA)
      If cell1 = vA(i) Then
        bFound = False
        For j = LBound(vB) To UBound(vB)
         If vB(j) = cell1.Offset(0, 1).Value Then
         bFound = True
         Exit For
         End If
        If Not bFound Then
         ReDim Preserve vB(1 To UBound(vB) + 1)
         vB(UBound(vB)) = cell1.Offset(0, 1).Value
         added = True
        End If
      End If
     Next i
     For i = LBound(vB) To UBound(vB)
      If cell1.Offset(0, 1).Value = vB(i) Then
        bFound = False
        For j = LBound(vA) To UBound(vA)
         If vA(j) = cell1.Value Then
         bFound = True
         Exit For
         End If
        If Not bFound Then
         ReDim Preserve vA(1 To UBound(vA) + 1)
         vA(UBound(vA)) = cell1.Value
         added = True
        End If
      End If
     Next i
   End If
  Next cell1
 Loop While added = True
 sA = ""
 sB = ""
 For i = LBound(vA) To UBound(vA)
   sA = sA & vA(i) & "&"
 sA = Left(sA, Len(sA) - 1)
 For j = LBound(vB) To UBound(vB)
   sB = sB & vB(j) & "&"
 sB = Left(sB, Len(sB) - 1)
 cell.Offset(0, 2).Value = sA
 cell.Offset(0, 3).Value = sB
Next cell
End Sub

It assumes your data starts in A2 as you show (with headers in row 1).

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.