uc1bear,
Here is an Excel Macro I created that reads in all of the columns of
the first three rows on the first worksheet, and places all of the
permutations on a new sheet in the same workbook.
Sub SetPermutations3Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intColumnWord1 As Integer, intColumnWord2 As Integer
Dim intColumnWord3 As Integer, intDataRow As Integer
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intColumnWord1 = 1
intColumnWord2 = 1
intColumnWord3 = 1
intDataRow = 1
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Select
Worksheets(1).Select
On Error Resume Next
Do Until IsEmpty(wksData.Cells(1, intColumnWord1))
Do Until IsEmpty(wksData.Cells(2, intColumnWord2))
Do Until IsEmpty(wksData.Cells(3, intColumnWord3))
word1 = wksData.Cells(1, intColumnWord1).Value
word2 = wksData.Cells(2, intColumnWord2).Value
word3 = wksData.Cells(3, intColumnWord3).Value
Worksheets(Worksheets.Count).Select
ActiveSheet.Cells(intDataRow, 1) = word1
ActiveSheet.Cells(intDataRow, 2) = word2
ActiveSheet.Cells(intDataRow, 3) = word3
intDataRow = intDataRow + 1
intColumnWord3 = intColumnWord3 + 1
Loop
intColumnWord3 = 1
intColumnWord2 = intColumnWord2 + 1
Loop
intColumnWord3 = 1
intColumnWord2 = 1
intColumnWord1 = intColumnWord1 + 1
Loop
Application.ScreenUpdating = True
End Sub
If you run the macro SetPermutations3Words with the following data on
the first worksheet:
A B C D E
-------- -------- -------- -------- --------
1 my his our
2 dog cat bird chicken
3 Spot Rex Stanley George Oscar
The output will display in the new worksheet as follows:
my dog Spot
my dog Rex
my dog Stanley
my dog George
my dog Oscar
my cat Spot
my cat Rex
my cat Stanley
my cat George
my cat Oscar
my bird Spot
my bird Rex
my bird Stanley
my bird George
my bird Oscar
my chicken Spot
my chicken Rex
my chicken Stanley
my chicken George
my chicken Oscar
his dog Spot
his dog Rex
his dog Stanley
his dog George
his dog Oscar
his cat Spot
his cat Rex
his cat Stanley
his cat George
his cat Oscar
his bird Spot
his bird Rex
his bird Stanley
his bird George
his bird Oscar
his chicken Spot
his chicken Rex
his chicken Stanley
his chicken George
his chicken Oscar
our dog Spot
our dog Rex
our dog Stanley
our dog George
our dog Oscar
our cat Spot
our cat Rex
our cat Stanley
our cat George
our cat Oscar
our bird Spot
our bird Rex
our bird Stanley
our bird George
our bird Oscar
our chicken Spot
our chicken Rex
our chicken Stanley
our chicken George
our chicken Oscar
I am also including SetPermutations4Words, with comments that will
show how to expand this out to any number of word permutations:
Sub SetPermutations4Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intColumnWord1 As Integer, intColumnWord2 As Integer
Dim intColumnWord3 As Integer, intDataRow As Integer
' Define Additional word counters as necessary
Dim intColumnWord4 As Integer
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Dim word4 As String
' Define Additional word strings as well
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intColumnWord1 = 1
intColumnWord2 = 1
intColumnWord3 = 1
' Initialize Additional word counters
intColumnWord4 = 1
intDataRow = 1
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Select
Worksheets(1).Select
On Error Resume Next
Do Until IsEmpty(wksData.Cells(1, intColumnWord1))
Do Until IsEmpty(wksData.Cells(2, intColumnWord2))
Do Until IsEmpty(wksData.Cells(3, intColumnWord3))
' Insert extra Do Loop for each additional word
Do Until IsEmpty(wksData.Cells(4, intColumnWord4))
word1 = wksData.Cells(1, intColumnWord1).Value
word2 = wksData.Cells(2, intColumnWord2).Value
word3 = wksData.Cells(3, intColumnWord3).Value
' Set additional word string for each additional word
word4 = wksData.Cells(4, intColumnWord4).Value
Worksheets(Worksheets.Count).Select
ActiveSheet.Cells(intDataRow, 1) = word1
ActiveSheet.Cells(intDataRow, 2) = word2
ActiveSheet.Cells(intDataRow, 3) = word3
' Set additional cell entry for each additional word
ActiveSheet.Cells(intDataRow, 4) = word4
intDataRow = intDataRow + 1
' Change this counter to the last word counter
' For example, for 5 words this would be intColumnWord5
intColumnWord4 = intColumnWord4 + 1
Loop
' Expand each closing counter reset to 1 for each new word
intColumnWord4 = 1
intColumnWord3 = intColumnWord3 + 1
Loop
intColumnWord4 = 1
intColumnWord3 = 1
intColumnWord2 = intColumnWord2 + 1
Loop
intColumnWord4 = 1
intColumnWord3 = 1
intColumnWord2 = 1
intColumnWord1 = intColumnWord1 + 1
Loop
Application.ScreenUpdating = True
End Sub
Running SetPermutations4Words on this list:
A B C D E
-------- -------- -------- -------- --------
1 my his our
2 dog cat bird chicken
3 Spot Rex Stanley George Oscar
4 Smith Goldberg Tanaka
Returns the following output:
my dog Spot Smith
my dog Spot Goldberg
my dog Spot Tanaka
my dog Rex Smith
my dog Rex Goldberg
my dog Rex Tanaka
my dog Stanley Smith
my dog Stanley Goldberg
my dog Stanley Tanaka
my dog George Smith
my dog George Goldberg
my dog George Tanaka
my dog Oscar Smith
my dog Oscar Goldberg
my dog Oscar Tanaka
my cat Spot Smith
my cat Spot Goldberg
my cat Spot Tanaka
my cat Rex Smith
my cat Rex Goldberg
my cat Rex Tanaka
my cat Stanley Smith
my cat Stanley Goldberg
my cat Stanley Tanaka
my cat George Smith
my cat George Goldberg
my cat George Tanaka
my cat Oscar Smith
my cat Oscar Goldberg
my cat Oscar Tanaka
my bird Spot Smith
my bird Spot Goldberg
my bird Spot Tanaka
my bird Rex Smith
my bird Rex Goldberg
my bird Rex Tanaka
my bird Stanley Smith
my bird Stanley Goldberg
my bird Stanley Tanaka
my bird George Smith
my bird George Goldberg
my bird George Tanaka
my bird Oscar Smith
my bird Oscar Goldberg
my bird Oscar Tanaka
my chicken Spot Smith
my chicken Spot Goldberg
my chicken Spot Tanaka
my chicken Rex Smith
my chicken Rex Goldberg
my chicken Rex Tanaka
my chicken Stanley Smith
my chicken Stanley Goldberg
my chicken Stanley Tanaka
my chicken George Smith
my chicken George Goldberg
my chicken George Tanaka
my chicken Oscar Smith
my chicken Oscar Goldberg
my chicken Oscar Tanaka
his dog Spot Smith
his dog Spot Goldberg
his dog Spot Tanaka
his dog Rex Smith
his dog Rex Goldberg
his dog Rex Tanaka
his dog Stanley Smith
his dog Stanley Goldberg
his dog Stanley Tanaka
his dog George Smith
his dog George Goldberg
his dog George Tanaka
his dog Oscar Smith
his dog Oscar Goldberg
his dog Oscar Tanaka
his cat Spot Smith
his cat Spot Goldberg
his cat Spot Tanaka
his cat Rex Smith
his cat Rex Goldberg
his cat Rex Tanaka
his cat Stanley Smith
his cat Stanley Goldberg
his cat Stanley Tanaka
his cat George Smith
his cat George Goldberg
his cat George Tanaka
his cat Oscar Smith
his cat Oscar Goldberg
his cat Oscar Tanaka
his bird Spot Smith
his bird Spot Goldberg
his bird Spot Tanaka
his bird Rex Smith
his bird Rex Goldberg
his bird Rex Tanaka
his bird Stanley Smith
his bird Stanley Goldberg
his bird Stanley Tanaka
his bird George Smith
his bird George Goldberg
his bird George Tanaka
his bird Oscar Smith
his bird Oscar Goldberg
his bird Oscar Tanaka
his chicken Spot Smith
his chicken Spot Goldberg
his chicken Spot Tanaka
his chicken Rex Smith
his chicken Rex Goldberg
his chicken Rex Tanaka
his chicken Stanley Smith
his chicken Stanley Goldberg
his chicken Stanley Tanaka
his chicken George Smith
his chicken George Goldberg
his chicken George Tanaka
his chicken Oscar Smith
his chicken Oscar Goldberg
his chicken Oscar Tanaka
our dog Spot Smith
our dog Spot Goldberg
our dog Spot Tanaka
our dog Rex Smith
our dog Rex Goldberg
our dog Rex Tanaka
our dog Stanley Smith
our dog Stanley Goldberg
our dog Stanley Tanaka
our dog George Smith
our dog George Goldberg
our dog George Tanaka
our dog Oscar Smith
our dog Oscar Goldberg
our dog Oscar Tanaka
our cat Spot Smith
our cat Spot Goldberg
our cat Spot Tanaka
our cat Rex Smith
our cat Rex Goldberg
our cat Rex Tanaka
our cat Stanley Smith
our cat Stanley Goldberg
our cat Stanley Tanaka
our cat George Smith
our cat George Goldberg
our cat George Tanaka
our cat Oscar Smith
our cat Oscar Goldberg
our cat Oscar Tanaka
our bird Spot Smith
our bird Spot Goldberg
our bird Spot Tanaka
our bird Rex Smith
our bird Rex Goldberg
our bird Rex Tanaka
our bird Stanley Smith
our bird Stanley Goldberg
our bird Stanley Tanaka
our bird George Smith
our bird George Goldberg
our bird George Tanaka
our bird Oscar Smith
our bird Oscar Goldberg
our bird Oscar Tanaka
our chicken Spot Smith
our chicken Spot Goldberg
our chicken Spot Tanaka
our chicken Rex Smith
our chicken Rex Goldberg
our chicken Rex Tanaka
our chicken Stanley Smith
our chicken Stanley Goldberg
our chicken Stanley Tanaka
our chicken George Smith
our chicken George Goldberg
our chicken George Tanaka
our chicken Oscar Smith
our chicken Oscar Goldberg
our chicken Oscar Tanaka |
Request for Answer Clarification by
uc1bear-ga
on
15 Apr 2004 01:43 PDT
Sounds like this is just what we were looking for, but I'm embarassed
to say I don't immediately know where/how to put the Marco into Excel
or execute it.
I am fairly well versed in Excel, though, so a very quick tutorial
should be fine. Thanks! uc
|
Request for Answer Clarification by
uc1bear-ga
on
15 Apr 2004 01:47 PDT
One other issue I just noticed...
Each of our datasets is going to have 1000s of elements in it, as I
mentioned. Thus, it would seem to be more ideal if we could list them
down columns versus across rows, as you have suggested.
is it possible to "flip" the macro, so we could actually have columns
with the input data, versus rows? Thanks
|
Clarification of Answer by
hailstorm-ga
on
15 Apr 2004 04:14 PDT
uc1bear,
Are you saying that each of the sets will be thousands of elements?
If so, then I'm afraid that an Excel solution will not work. If each
set contains 1,000 words, then for three sets you will have
1,000,000,000 combinations, and there are only a little more than
4,000,000 cells in an Excel spreadsheet.
I will look into exporting the results to a CSV file instead.
|
Clarification of Answer by
hailstorm-ga
on
15 Apr 2004 05:13 PDT
uc1bear,
To write a macro into a spreadsheet, open the spreadsheet at press
Alt-F11. On the left side menu, doubleclick on "ThisWorkbook" to
bring up the code window. Then paste the entire code into the macro
and save the workbook.
Once you go back to the spreadsheet display, press Alt-F8 to run the macro.
Here is the code for the data in columns instead of rows:
Sub SetPermutations3Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intRowWord1 As Integer, intRowWord2 As Integer
Dim intRowWord3 As Integer, intDataRow As Integer
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intRowWord1 = 1
intRowWord2 = 1
intRowWord3 = 1
intDataRow = 1
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Select
Worksheets(1).Select
On Error Resume Next
Do Until IsEmpty(wksData.Cells(intRowWord1, 1))
Do Until IsEmpty(wksData.Cells(intRowWord2, 2))
Do Until IsEmpty(wksData.Cells(intRowWord3, 3))
word1 = wksData.Cells(intRowWord1, 1).Value
word2 = wksData.Cells(intRowWord2, 2).Value
word3 = wksData.Cells(intRowWord3, 3).Value
Worksheets(Worksheets.Count).Select
ActiveSheet.Cells(intDataRow, 1) = word1
ActiveSheet.Cells(intDataRow, 2) = word2
ActiveSheet.Cells(intDataRow, 3) = word3
intDataRow = intDataRow + 1
intRowWord3 = intRowWord3 + 1
Loop
intRowWord3 = 1
intRowWord2 = intRowWord2 + 1
Loop
intRowWord3 = 1
intRowWord2 = 1
intRowWord1 = intRowWord1 + 1
Loop
Application.ScreenUpdating = True
End Sub
However, as mentioned before this may not work properly with the large
amount you may be looking to create. Could you please confirm whether
you expect to have thousands of entries per set?
|
Clarification of Answer by
hailstorm-ga
on
15 Apr 2004 16:02 PDT
uc1bear,
After testing, the previous code does not work for sets that will
result in over 32,767 rows of data. This revised code will work for
65,536, the maximum number of rows in Excel, or roughly 40 entries per
set for three sets:
Sub SetPermutations3Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intRowWord1 As Integer, intRowWord2 As Integer
Dim intRowWord3 As Integer, intDataRow As Long
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intRowWord1 = 1
intRowWord2 = 1
intRowWord3 = 1
intDataRow = 1
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Select
Worksheets(1).Select
On Error Resume Next
Do Until IsEmpty(wksData.Cells(intRowWord1, 1))
Do Until IsEmpty(wksData.Cells(intRowWord2, 2))
Do Until IsEmpty(wksData.Cells(intRowWord3, 3))
word1 = wksData.Cells(intRowWord1, 1).Value
word2 = wksData.Cells(intRowWord2, 2).Value
word3 = wksData.Cells(intRowWord3, 3).Value
Worksheets(Worksheets.Count).Select
ActiveSheet.Cells(intDataRow, 1) = word1
ActiveSheet.Cells(intDataRow, 2) = word2
ActiveSheet.Cells(intDataRow, 3) = word3
intDataRow = intDataRow + 1
intRowWord3 = intRowWord3 + 1
Loop
intRowWord3 = 1
intRowWord2 = intRowWord2 + 1
Loop
intRowWord3 = 1
intRowWord2 = 1
intRowWord1 = intRowWord1 + 1
Loop
Application.ScreenUpdating = True
End Sub
|
Clarification of Answer by
hailstorm-ga
on
15 Apr 2004 23:57 PDT
uc1bear,
I have modified the code to write the results of all permutations of
the three columns to a text file called "all_results.txt" that will be
located in the same folder as the Excel file:
Sub SetPermutations3Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intRowWord1 As Integer, intRowWord2 As Integer
Dim intRowWord3 As Integer, intDataRow As Long
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intRowWord1 = 1
intRowWord2 = 1
intRowWord3 = 1
intDataRow = 1
Worksheets(1).Select
Open ThisWorkbook.Path & "\" & "all_phrases.txt" For Output As #1
On Error Resume Next
Do Until IsEmpty(wksData.Cells(intRowWord1, 1))
Do Until IsEmpty(wksData.Cells(intRowWord2, 2))
Do Until IsEmpty(wksData.Cells(intRowWord3, 3))
word1 = wksData.Cells(intRowWord1, 1).Value
word2 = wksData.Cells(intRowWord2, 2).Value
word3 = wksData.Cells(intRowWord3, 3).Value
Print #1, word1 & " " & word2 & " " & word3 & " "
intDataRow = intDataRow + 1
intRowWord3 = intRowWord3 + 1
Loop
intRowWord3 = 1
intRowWord2 = intRowWord2 + 1
Loop
intRowWord3 = 1
intRowWord2 = 1
intRowWord1 = intRowWord1 + 1
Loop
Close #1
End Sub
I have tested this with three columns of 100 words each. An
approximately 16MB text file with 1,000,000 permutations was created
in about three minutes on my PC. To do this for three columns of 1000
words each would increase those calculations by a factor of 1000,
meaning that this would take over a day to create a text file between
10 and 30 GB, depending on the size of the words used. If you are
only doing this once or twice this may suffice for your needs, but if
you will be performing this operation multiple times then we will have
to look at something other than Excel to provide a more efficient
solution.
|
Request for Answer Clarification by
uc1bear-ga
on
17 Apr 2004 01:07 PDT
creating the macro was a breeze. i tested three small sets (10 terms
each) and it worked great. excellent answer. you do fine work!
one small issue, i wrongfully assumed that if i could use this macro
for two-set permutations as well, but if the third column is empty,
the macro spits out a blank text file.
would a two-setter be a different program? or could the one we're
working with be modified to do two OR three set permutations? either
solution would suffice. i'd be happy to toss in a $5.00 tip for
two-set capability.
|
Clarification of Answer by
hailstorm-ga
on
17 Apr 2004 03:21 PDT
uc1bear,
I'm glad that you are satisfied with the answer. As requested, here
is the macro for 2 word permutations, followed by the macro for 4 word
permutations. Just select the one you wish to run after pressing
Alt-F8 to start the macro.
Sub SetPermutations2Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intRowWord1 As Integer, intRowWord2 As Integer
Dim intDataRow As Long
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intRowWord1 = 1
intRowWord2 = 1
intDataRow = 1
Worksheets(1).Select
Open ThisWorkbook.Path & "\" & "all_phrases.txt" For Output As #1
On Error Resume Next
Do Until IsEmpty(wksData.Cells(intRowWord1, 1))
Do Until IsEmpty(wksData.Cells(intRowWord2, 2))
word1 = wksData.Cells(intRowWord1, 1).Value
word2 = wksData.Cells(intRowWord2, 2).Value
Print #1, word1 & " " & word2
intDataRow = intDataRow + 1
intRowWord2 = intRowWord2 + 1
Loop
intRowWord2 = 1
intRowWord1 = intRowWord1 + 1
Loop
Close #1
End Sub
Sub SetPermutations4Words()
Dim wks As Worksheet, wksData As Worksheet
Dim intRowWord1 As Integer, intRowWord2 As Integer
Dim intRowWord3 As Integer, intRowWord4 As Integer, intDataRow As Long
Dim strSheet As String
Dim word1 As String
Dim word2 As String
Dim word3 As String
Dim word4 As String
Application.ScreenUpdating = False
Set wksData = ActiveSheet
intRowWord1 = 1
intRowWord2 = 1
intRowWord3 = 1
intRowWord4 = 1
intDataRow = 1
Worksheets(1).Select
Open ThisWorkbook.Path & "\" & "all_phrases.txt" For Output As #1
On Error Resume Next
Do Until IsEmpty(wksData.Cells(intRowWord1, 1))
Do Until IsEmpty(wksData.Cells(intRowWord2, 2))
Do Until IsEmpty(wksData.Cells(intRowWord3, 3))
Do Until IsEmpty(wksData.Cells(intRowWord4, 4))
word1 = wksData.Cells(intRowWord1, 1).Value
word2 = wksData.Cells(intRowWord2, 2).Value
word3 = wksData.Cells(intRowWord3, 3).Value
word4 = wksData.Cells(intRowWord4, 4).Value
Print #1, word1 & " " & word2 & " " & word3 & " " & word4
intDataRow = intDataRow + 1
intRowWord4 = intRowWord4 + 1
Loop
intRowWord4 = 1
intRowWord3 = intRowWord3 + 1
Loop
intRowWord4 = 1
intRowWord3 = 1
intRowWord2 = intRowWord2 + 1
Loop
intRowWord4 = 1
intRowWord3 = 1
intRowWord2 = 1
intRowWord1 = intRowWord1 + 1
Loop
Close #1
End Sub
|