You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- Extract variable entangled content

Advertisement

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

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

Do

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

Next

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

Next

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) & "&"

Next

sA = Left(sA, Len(sA) - 1)

For j = LBound(vB) To UBound(vB)

sB = sB & vB(j) & "&"

Next

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).

--

Regards,

Tom Ogilvy

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Oh my goodness, another flawless reply from Tom. Thank you, thank you, thank you. The last time I asked Tom was a couple of years ago when I hoped he would become my last resort on an issue I had with a macro. It turned out it was a piece of cake for Tom. I was relieved then, and to be honest, I was already pretty relieved now when Tom's initial response stated it wasn't really possible to make a straightaway reply because of its complexity. I resigned to my fate having to execute monthly manual calculations. Well, I thought, only a workable solution would have been the better answer, and then Tom followed it up by exactly that! The matter had haunted me for the past months. As Tom guessed correctly, the values were simplified. Column A actually displays shipment Ids, that relates to volumes in another column. Column B lists container IDs similarly relating to container load capacity in another column. Target: calculate the load factor per container. Formulas to calculate the load factor for different scenarios are not too difficult though. To extract the applicable scenario per row was, at least for me. Having received Tom's brilliant reply I feel so excited I can start or perhaps better continue fiddle things around applying this macro that worked perfectly when I tested it for a number of (extended) scenarios. I'm sure I don't yet understand all the elements of the macro you constructed, but I'm also sure that when I'm going to work with it, the understanding will gradually improve, like it did last time when you helped me out. Although I felt and feel somewhat reluctant to ask questions directly to Tom, I am also very grateful that Tom again took the time to relay such a great answer, and becoming my last resort, again... Tom, you're amazing. That your enlightening insight may shine upon us for many more years. Kind regards, Joe |

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

Excel

Answers by Expert:

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. **Education/Credentials**

Master of Science (MS) degree Operations Research (ORSA)**Awards and Honors**

Microsoft MVP in Excel.