The following is based on my solution here:
http://216.92.17.166/board2/viewtopic.php?t=44585
I'll put a new version of the code anyway....
Based on this worksheet:
Create unique strings with recursion.xls |
---|
|
---|
| A | B | C | D |
---|
1 | ColumnA | ColumnB | ColumnC | |
---|
2 | TermA1 | TermB1 | TermC1 | |
---|
3 | TermA2 | TermB2 | TermC2 | |
---|
4 | TermA3 | TermB3 | | |
---|
5 | TermA4 | | | |
---|
6 | TermA5 | | | |
---|
|
---|
I created the following scenario next to it:
where F1 is named 'Concat', F2 'Separator', F3 'InsSeparator' and F5 'Result'
The value of F1 was created using MCONCAT from MoreFunc.xll but that is not the important thing, the important is to create a string that has the same structure (Each member of the column separated by "InsSeparator" and those three strings separated by "Separator")
Next, I run the macro
Test to get the results:<font face=Courier New><SPAN style="color:darkblue">Option</SPAN><SPAN style="color:darkblue">Explicit</SPAN><SPAN style="color:darkblue">Sub</SPAN> Test()
<SPAN style="color:darkblue">Dim</SPAN> Ans<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Boolean</SPAN>, Col<SPAN style="color:darkblue">As</SPAN> Collection, i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>
<SPAN style="color:darkblue">Set</SPAN> Col =<SPAN style="color:darkblue">New</SPAN> Collection
Ans = BuildLoops(Range("Concat").Value, Range("Separator").Value, _
Range("InsSeparator").Value, Col)
<SPAN style="color:darkblue">If</SPAN> Ans<SPAN style="color:darkblue">Then</SPAN>
<SPAN style="color:darkblue">With</SPAN> Range("Result")
<SPAN style="color:darkblue">For</SPAN> i = 1<SPAN style="color:darkblue">To</SPAN> Col.Count
.Offset(i).Value = Col(i)
<SPAN style="color:darkblue">Next</SPAN> i
<SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">Else</SPAN>
MsgBox "Error !", vbCritical
<SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Sub</SPAN><SPAN style="color:darkblue">Function</SPAN> BuildLoops(<SPAN style="color:darkblue">ByVal</SPAN> St<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
<SPAN style="color:darkblue">ByVal</SPAN> Sep<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
<SPAN style="color:darkblue">ByVal</SPAN> Sep2<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
<SPAN style="color:darkblue">ByRef</SPAN> Col<SPAN style="color:darkblue">As</SPAN> Collection)<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Boolean</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> Ar<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, Ar2<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, ArMain<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>
<SPAN style="color:darkblue">Dim</SPAN> i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, j<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, Ctr<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, TempSt<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>
St = Application.Substitute(St, " ", "")
Ar = Split(St, Sep)
<SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">Not</SPAN> IsArray(Ar)<SPAN style="color:darkblue">Then</SPAN><SPAN style="color:darkblue">Exit</SPAN><SPAN style="color:darkblue">Function</SPAN>
<SPAN style="color:darkblue">ReDim</SPAN> ArMain(1<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(Ar) -<SPAN style="color:darkblue">LBound</SPAN>(Ar) + 1)
<SPAN style="color:darkblue">For</SPAN> i =<SPAN style="color:darkblue">LBound</SPAN>(Ar)<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(Ar)
Ar2 = Split(Ar(i), Sep2)
Ctr = Ctr + 1
ArMain(Ctr) = Ar2
<SPAN style="color:darkblue">Next</SPAN> i
<SPAN style="color:darkblue">For</SPAN> j =<SPAN style="color:darkblue">LBound</SPAN>(ArMain(1))<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(ArMain(1))
TempSt = ArMain(1)(j)
BuildString 1, ArMain, Col, TempSt, Sep
<SPAN style="color:darkblue">Next</SPAN> j
BuildLoops =<SPAN style="color:darkblue">True</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Function</SPAN><SPAN style="color:darkblue">Private</SPAN><SPAN style="color:darkblue">Function</SPAN> BuildString(<SPAN style="color:darkblue">ByRef</SPAN> i<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, _
<SPAN style="color:darkblue">ByRef</SPAN> ArMain<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Variant</SPAN>, _
<SPAN style="color:darkblue">ByRef</SPAN> Col<SPAN style="color:darkblue">As</SPAN> Collection, _
<SPAN style="color:darkblue">ByRef</SPAN> TempSt<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>, _
<SPAN style="color:darkblue">ByRef</SPAN> Sep<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>)
<SPAN style="color:darkblue">Dim</SPAN> j<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">Long</SPAN>, St<SPAN style="color:darkblue">As</SPAN><SPAN style="color:darkblue">String</SPAN>
St = TempSt
<SPAN style="color:darkblue">If</SPAN> i<<SPAN style="color:darkblue">UBound</SPAN>(ArMain)<SPAN style="color:darkblue">Then</SPAN>
<SPAN style="color:darkblue">For</SPAN> j =<SPAN style="color:darkblue">LBound</SPAN>(ArMain(i + 1))<SPAN style="color:darkblue">To</SPAN><SPAN style="color:darkblue">UBound</SPAN>(ArMain(i + 1))
TempSt = St & Sep & ArMain(i + 1)(j)
BuildString i + 1, ArMain, Col, TempSt, Sep
<SPAN style="color:darkblue">Next</SPAN> j
<SPAN style="color:darkblue">Else</SPAN>
Col.Add TempSt
<SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">If</SPAN><SPAN style="color:darkblue">End</SPAN><SPAN style="color:darkblue">Function</SPAN></FONT>