Combining Text in Columsn
I'm trying to produce a macro that would output all
combinations of words from multiple lists. So,
Hi. Here's another of many ways to do an "Outer" function.
This example is on 3 columns, but you can do more.
Sub YourMainCode()
Dim v
v = Outer([A1:A3], [B1:B2], [C1:C4])
[D1].Resize(UBound(v)) = v
[D:D].EntireColumn.AutoFit
End Sub
Function Outer(ParamArray v() As Variant)
'// Dana DeLouis
Dim C As Long
Dim J As Long
Dim K As Long
Dim s As String
Dim Sp As String
Dim R As Variant
Dim N As Variant
Dim d As Variant
Sp = Space(1)
Set d = CreateObject("Scripting.Dictionary")
R = T1(v(0))
For C = 1 To UBound(v)
N = T1(v(C))
For J = LBound(R) To UBound(R)
For K = LBound(N) To UBound(N)
s = R(J) & Sp & N(K)
d.Add d.Count, s
Next K
Next J
R = d.Items
d.RemoveAll
Next C
Set d = Nothing
Outer = T1(R)
End Function
Private Function T1(v)
' Transpose 1 time.
T1 = WorksheetFunction.Transpose(v)
End Function
--
HTH :)
Dana DeLouis
Windows XP & Excel 2007
wrote in message
ups.com...
Hi,
I've found variations of what I'm looking for in the group, but
nothing that's an exact fit.
I'm trying to produce a macro that would output all combinations of
words from multiple lists. So, for example, if a sheet contains the
following two lists:
A B
1 happy cow
2 sad dog
3 angry cat
The code would produce the following list in Column C (words separated
by a space).
C
1 happy cow
2 happy dog
3 happy cat
4 sad cow
5 sad dog
6 sad cat
7 angry cow
8 angry dog
9 angry cat
The macro should be able to perform this routine for two, three, or
four columns of words (placing the completed list in the column
immediately to the right of the last list).
Any ideas how to do this?
Thanks!
|