View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
[email protected] mattisokayatexcel@gmail.com is offline
external usenet poster
 
Posts: 10
Default Combining Text in Columsn

On Jul 29, 6:34 pm, Tom Ogilvy
wrote:
at the end of the macro, delete the columns you don't want.

--
Regards,
Tom Ogilvy

" wrote:
On Jul 29, 1:59 pm, "squenson via OfficeKB.com" <u36146@uwe wrote:
Here is a macro that does it for up to 6 columns. The list of words starts in
row 1 and there should not be blank. Results are copied in column G.


Sub GenerateSentences()


Dim i1 As Long
Dim i2 As Long
Dim i3 As Long
Dim i4 As Long
Dim i5 As Long
Dim i6 As Long


i1 = 1
i2 = 1
i3 = 1
i4 = 1
i5 = 1
i6 = 1
j = 1
While Cells(i1, 1) < "" Or i1 = 1
While Cells(i2, 2) < "" Or i2 = 1
While Cells(i3, 3) < "" Or i3 = 1
While Cells(i4, 4) < "" Or i4 = 1
While Cells(i5, 5) < "" Or i5 = 1
While Cells(i6, 6) < "" Or i6 = 1
Cells(j, 7) = Trim(Cells(i1, 1) & _
" " & Cells(i2, 2) & " " & Cells(i3, 3) & " " & _
Cells(i4, 4) & " " & Cells(i5, 5) & " " & Cells(i6, 6))
j = j + 1
i6 = i6 + 1
Wend
i6 = 1
i5 = i5 + 1
Wend
i5 = 1
i4 = i4 + 1
Wend
i4 = 1
i3 = i3 + 1
Wend
i3 = 1
i2 = i2 + 1
Wend
i2 = 1
i1 = i1 + 1
Wend


End Sub


wrote:
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!


--
Message posted viahttp://www.officekb.com


This is phenomenal! Exactly what I was looking for.


I modified the code slightly so that it only handles between two and
four columns of words (see below; there shouldn't be more than this).
On a slightly cosmetic note, is there a way to paste the completed
list two columns to the right of the last list of words, instead of in
column G? So, if there are two columns of words, the final list will
appear in column D; if there are three columns, it will show up in
column E; if there are four, it will be in column F.


Thanks!


Modified:


Sub GenerateSentences()


Dim i1 As Long
Dim i2 As Long
Dim i3 As Long
Dim i4 As Long


i1 = 1
i2 = 1
i3 = 1
i4 = 1
J = 1
While Cells(i1, 1) < "" Or i1 = 1
While Cells(i2, 2) < "" Or i2 = 1
While Cells(i3, 3) < "" Or i3 = 1
While Cells(i4, 4) < "" Or i4 = 1
Cells(J, 5) = Trim(Cells(i1, 1) & _
" " & Cells(i2, 2) & " " & Cells(i3, 3) & " " & _
Cells(i4, 4))
J = J + 1
i4 = i4 + 1
Wend
i4 = 1
i3 = i3 + 1
Wend
i3 = 1
i2 = i2 + 1
Wend
i2 = 1
i1 = i1 + 1
Wend


End Sub


How would I have the macro find the last column from the left with
text, then insert the outputted list two columns to the right of this?