MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2002, 10:53 AM   #1
ExcelHelp
New Member
 
Join Date: Apr 2002
Posts: 2
Default

I want to create every possible combination of numers from four columns of data. for example if the data looks like this:
A B C D
2 1 5 8
3 2 6 9
4 3 7 10

then I would like to see a list like the following:

2158
3158
4158
2269
3369
4469
etc.

until all possible combinations are created. Does Excel have a function that would help with this?
ExcelHelp is offline   Reply With Quote
Old Apr 18th, 2002, 01:56 PM   #2
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

4469 ?

where did that come from ?
Chris Davison is offline   Reply With Quote
Old Apr 18th, 2002, 02:46 PM   #3
ExcelHelp
New Member
 
Join Date: Apr 2002
Posts: 2
Default

Quote:
On 2002-04-18 12:56, Chris Davison wrote:
4469 ?

where did that come from ?
sorry the list should be:

2158
3158
4158
2269
3269
4269
etc
ExcelHelp is offline   Reply With Quote
Old Apr 18th, 2002, 03:49 PM   #4
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

so it's any 4 numbers from any row in any column ?

looks like a VBA solution is needed so I'll bow out of this one, although I'll mull it over in between rebootings tommorrow

actually, just so we're clear.... how many rows does this extend down to and how many actual columns also ?

like....is it 10x5 or are we talking 3,000x50 ?!

[ This Message was edited by: Chris Davison on 2002-04-18 14:50 ]
Chris Davison is offline   Reply With Quote
Old Apr 18th, 2002, 04:03 PM   #5
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Here's the results...

{2,1,5,8;3,1,5,8;4,1,5,8
;2,2,5,8;3,2,5,8;4,2,5,8
;2,3,5,8;3,3,5,8;4,3,5,8
;2,1,6,8;3,1,6,8;4,1,6,8
;2,2,6,8;3,2,6,8;4,2,6,8
;2,3,6,8;3,3,6,8;4,3,6,8
;2,1,7,8;3,1,7,8;4,1,7,8
;2,2,7,8;3,2,7,8;4,2,7,8
;2,3,7,8;3,3,7,8;4,3,7,8
;2,1,5,9;3,1,5,9;4,1,5,9
;2,2,5,9;3,2,5,9;4,2,5,9
;2,3,5,9;3,3,5,9;4,3,5,9
;2,1,6,9;3,1,6,9;4,1,6,9
;2,2,6,9;3,2,6,9;4,2,6,9
;2,3,6,9;3,3,6,9;4,3,6,9
;2,1,7,9;3,1,7,9;4,1,7,9
;2,2,7,9;3,2,7,9;4,2,7,9
;2,3,7,9;3,3,7,9;4,3,7,9
;2,1,5,10;3,1,5,10;4,1,5,10
;2,2,5,10;3,2,5,10;4,2,5,10
;2,3,5,10;3,3,5,10;4,3,5,10
;2,1,6,10;3,1,6,10;4,1,6,10
;2,2,6,10;3,2,6,10;4,2,6,10
;2,3,6,10;3,3,6,10;4,3,6,10
;2,1,7,10;3,1,7,10;4,1,7,10
;2,2,7,10;3,2,7,10;4,2,7,10
;2,3,7,10;3,3,7,10;4,3,7,10}

It's a cartesian product where each of the columns is treated as a 1-column table using the Excel ODBC driver and the following SQL...

SELECT Table1.F1, Table2.F2, Table3.F3, Table4.F4
FROM Table1, Table2, Table3, Table4

[ This Message was edited by: Mark W. on 2002-04-18 15:11 ]
Mark W. is offline   Reply With Quote
Old Apr 18th, 2002, 04:17 PM   #6
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

Mark,

with 3 unlike digits in each of the 4 rows, there's a possible 3^4 combinations...

or 81

your array has 81 answers



(during my cigarette break today, I was reading up on SQL as it's dribbling into my daily work more often now.... one of the first things it said though was that the SQL statement should always end with a ";"

[notes written in 1998 though]

ie SELECT * from asuheader WHERE client='MW';

are they out of date ?

thanks
nice SQL that
Chris Davison is offline   Reply With Quote
Old Apr 18th, 2002, 04:26 PM   #7
Mark W.
MrExcel MVP
 
Join Date: Feb 2002
Location: Austin, Texas USA
Posts: 11,654
Default

Quote:
On 2002-04-18 15:17, Chris Davison wrote:
.... one of the first things it said though was that the SQL statement should always end with a ";"

[notes written in 1998 though]

ie SELECT * from asuheader WHERE client='MW';

are they out of date ?
Oracle requires it, but I did mine in Microsoft Query which doesn't. I trust you understand what makes this a Cartesian Product -- no WHERE clause; therefore, no table joins! Ordinarily, this would be a BIG No-No, but it "fits this problem to a tee". Happy computing.

[ This Message was edited by: Mark W. on 2002-04-18 15:28 ]
Mark W. is offline   Reply With Quote
Old Apr 18th, 2002, 04:29 PM   #8
Chris Davison
MrExcel MVP
 
Join Date: Feb 2002
Location: Millbank, London, UK
Posts: 1,790
Default

Ahhh, yeah, my stuff is in an oracle database, or I assume it is, everything keeps saying ODBC...... all double dutch to me at the moment ! cheers
Chris Davison is offline   Reply With Quote
Old Apr 18th, 2002, 05:01 PM   #9
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

I must say that the SQL solution is genius.
I wish i could have dreamt that one up.
My VBA-solution is much longer, but still quick.
Regards Tommy


Sub test2()
Set AWF = Application.WorksheetFunction
Dim Matrix As Variant
Dim NyMatrix As Variant
Dim NoOfRowMatrix
Dim MaxColumns, MaxRows As Integer
Dim totalRows, repetion, a, i As Long
Dim z, x, y As Byte
MaxRows = 0
totalRows = 1
Set inarea = Application.InputBox("Input range ?", Type:=8)
Set outarea = Application.InputBox("First Outputcell ?", Type:=8)
With AWF
MaxColumns = inarea.Columns.Count
ReDim NoOfRowMatrix(MaxColumns)
For x = 1 To MaxColumns
NoOfRowMatrix(x) = .CountA(Columns(x)) - 1
If NoOfRowMatrix(x) > MaxRows Then MaxRows = NoOfRowMatrix(x)
totalRows = totalRows * NoOfRowMatrix(x)
Next x
End With
If totalRows > 65000 Then Exit Sub
ReDim Matrix(MaxRows, MaxColumns)
Matrix = inarea

ReDim NyMatrix(totalRows, MaxColumns)
With Worksheets("Ark2")
For z = 1 To MaxColumns
repetion = 1
For x = z + 1 To MaxColumns
repetion = repetion * NoOfRowMatrix(x)
Next x
a = 1
While a <= totalRows
For y = 1 To NoOfRowMatrix(z)
For i = 1 To repetion
NyMatrix(a, z) = Matrix(y, z)
a = a + 1
Next i
Next y
Wend
Next z
Range(outarea, outarea.Offset(totalRows - 1, MaxColumns - 1)) = NyMatrix
MsgBox ("Finished with " & totalRows)
End With
End Sub
Tommy Bak is offline   Reply With Quote
Old Apr 19th, 2002, 03:42 AM   #10
Tommy Bak
Board Regular
 
Join Date: Feb 2002
Location: Guderup, Denmark
Posts: 287
Default

Sorry to have posted this rubbish. I've cleaned it a bit.

Sub kombinationer()
Dim Matrix As Variant
Dim NyMatrix As Variant
Dim NoOfRowMatrix
Dim MaxColumns, MaxRows As Integer
Dim totalRows, repetion, a, i As Long
Dim z, x, y As Byte
MaxRows = 0
totalRows = 1
Set inarea = Application.InputBox("Input range ?", Type:=8)
Set outarea = Application.InputBox("First Outputcell ?", Type:=8)
MaxColumns = inarea.Columns.Count
MaxRows = inarea.Rows.Count
ReDim NoOfRowMatrix(MaxColumns)
For x = 1 To MaxColumns
For y = 1 To MaxRows
If Not IsEmpty(inarea.Cells(y, x)) Then NoOfRowMatrix(x) = NoOfRowMatrix(x) + 1
Next y
totalRows = totalRows * NoOfRowMatrix(x)
Next x

If totalRows > 65000 Then Exit Sub
ReDim Matrix(MaxRows, MaxColumns)
Matrix = inarea

ReDim NyMatrix(totalRows, MaxColumns)
For z = 1 To MaxColumns
repetion = 1
For x = z + 1 To MaxColumns
repetion = repetion * NoOfRowMatrix(x)
Next x
a = 1
While a <= totalRows
For y = 1 To NoOfRowMatrix(z)
For i = 1 To repetion
NyMatrix(a, z) = Matrix(y, z)
a = a + 1
Next i
Next y
Wend
Next z
Range(outarea, outarea.Offset(totalRows - 1, MaxColumns - 1)) = NyMatrix
MsgBox ("Finished with " & totalRows)
End Sub
Tommy Bak is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 07:41 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes