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 May 1st, 2002, 04:54 AM   #1
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

I want to create a macro that gives me tree
different numbers between 1 and 25
But the difference between this numbers is not greater then 15
This macro must give me all possibility combinations in the column A1 to A????
Thanks for help
verluc is offline   Reply With Quote
Old May 1st, 2002, 08:43 AM   #2
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

Quote:
On 2002-05-01 03:54, verluc wrote:
I want to create a macro that gives me tree
different numbers between 1 and 25
But the difference between this numbers is not greater then 15
This macro must give me all possibility combinations in the column A1 to A????
Thanks for help
An example :

1 2 3 CORRECT
1 2 18 INCORRECT
verluc is offline   Reply With Quote
Old May 1st, 2002, 08:59 AM   #3
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default


I made the assumption in your problem statement that no two numbers could be more than 15 apart (and that 15 apart was OK). If the acutal problem is that no number can be more than 15 more than one other number, the code is different.

For example, I assumed 1,8,17 was not OK since 1 and 17 were too far apart. If this is the case, then the following code will generate the 9030 rows of numbers.

Sub Create_Numbers()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iCount As Integer

iCount = 1

For i = 1 To 25
For j = 1 To 25
For k = 1 To 25
If i <> j And _
i <> k And _
j <> k And _
Abs(i - k) <= 15 And _
Abs(j - k) <= 15 And _
Abs(i - j) <= 15 Then
Cells(iCount, 1).Value = i
Cells(iCount, 2).Value = j
Cells(iCount, 3).Value = k
iCount = iCount + 1
End If

Next
Next
Next

End Sub

To use the other case (where every number must be at minimum 15 away from another number), the if statement would change to:

If i <> j And _
i <> k And _
j <> k And _
(Abs(i - k) <= 15 Or _
Abs(j - k) <= 15 Or _
Abs(i - j) <= 15) Then

and would return the same as if there were no <= 15 condtions at all.

Hope that makes sense...

K
kkknie is offline   Reply With Quote
Old May 1st, 2002, 09:15 AM   #4
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

Quote:
On 2002-05-01 07:59, kkknie wrote:

I made the assumption in your problem statement that no two numbers could be more than 15 apart (and that 15 apart was OK). If the acutal problem is that no number can be more than 15 more than one other number, the code is different.

For example, I assumed 1,8,17 was not OK since 1 and 17 were too far apart. If this is the case, then the following code will generate the 9030 rows of numbers.

Sub Create_Numbers()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iCount As Integer

iCount = 1

For i = 1 To 25
For j = 1 To 25
For k = 1 To 25
If i <> j And _
i <> k And _
j <> k And _
Abs(i - k) <= 15 And _
Abs(j - k) <= 15 And _
Abs(i - j) <= 15 Then
Cells(iCount, 1).Value = i
Cells(iCount, 2).Value = j
Cells(iCount, 3).Value = k
iCount = iCount + 1
End If

Next
Next
Next

End Sub

To use the other case (where every number must be at minimum 15 away from another number), the if statement would change to:

If i <> j And _
i <> k And _
j <> k And _
(Abs(i - k) <= 15 Or _
Abs(j - k) <= 15 Or _
Abs(i - j) <= 15) Then

and would return the same as if there were no <= 15 condtions at all.

Hope that makes sense...

K
Hi friend,

I thank you for your time.
When I run the second macro,then I get the
following numbers:

1 2 18

the difference between those numbers is more then 15 : see 2 and 18
Can you change your macro?
(the difference between all the 3 numbers may no be more then 15)
Thanks
verluc is offline   Reply With Quote
Old May 4th, 2002, 04:38 AM   #5
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

Quote:
On 2002-05-01 08:15, verluc wrote:
Quote:
On 2002-05-01 07:59, kkknie wrote:

I made the assumption in your problem statement that no two numbers could be more than 15 apart (and that 15 apart was OK). If the acutal problem is that no number can be more than 15 more than one other number, the code is different.

For example, I assumed 1,8,17 was not OK since 1 and 17 were too far apart. If this is the case, then the following code will generate the 9030 rows of numbers.

Sub Create_Numbers()

Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim iCount As Integer

iCount = 1

For i = 1 To 25
For j = 1 To 25
For k = 1 To 25
If i <> j And _
i <> k And _
j <> k And _
Abs(i - k) <= 15 And _
Abs(j - k) <= 15 And _
Abs(i - j) <= 15 Then
Cells(iCount, 1).Value = i
Cells(iCount, 2).Value = j
Cells(iCount, 3).Value = k
iCount = iCount + 1
End If

Next
Next
Next

End Sub

To use the other case (where every number must be at minimum 15 away from another number), the if statement would change to:

If i <> j And _
i <> k And _
j <> k And _
(Abs(i - k) <= 15 Or _
Abs(j - k) <= 15 Or _
Abs(i - j) <= 15) Then

and would return the same as if there were no <= 15 condtions at all.

Hope that makes sense...

K
Hi friend,

I thank you for your time.
When I run the second macro,then I get the
following numbers:

1 2 18

the difference between those numbers is more then 15 : see 2 and 18
Can you change your macro?
(the difference between all the 3 numbers may no be more then 15)
Thanks
Any idea to change this macro?
Thanks
verluc is offline   Reply With Quote
Old May 4th, 2002, 08:52 AM   #6
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

The next number in the sequence is bounded by +/- 15 from the prior number, correct?

Here are two routines to try. They are identical, except that the second one calls the RandBetween function in VBA. You must set a reference to the Analysis ToolPak to call it.

From the VBE
Tools>References>check atpvbaen.xls

Code:
Sub bounded_random()
Dim x As Integer
Randomize

Cells(1, 1) = Int(Rnd * 25) + 1
For x = 2 To 1000
    Cells(x, 1) = Int(Rnd * 25) + 1
    Do Until Abs(Cells(x, 1) - Cells(x - 1, 1)) <= 15
        Cells(x, 1) = Int(Rnd * 25) + 1
    Loop
Next x

End Sub

Sub bounded_random2()
Dim x As Integer
Randomize

Cells(1, 1) = randbetween(1, 25)
For x = 2 To 1000
    Cells(x, 1) = randbetween(1, 25)
    Do Until Abs(Cells(x, 1) - Cells(x - 1, 1)) <= 15
        Cells(x, 1) = randbetween(1, 25)
    Loop
Next x

End Sub
This places 1000 numbers in column A. Modify as needed.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 4th, 2002, 10:26 AM   #7
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

It would probably help if I read the question thoroughly.

You want an exhaustive list with the changes bounded? That means hypergeometric sampling (sampling without replacement).

The following routine might get closer to your goal.

Code:
Sub bounded_random()
Dim x As Long, samplesize As Long
Randomize

samplesize = 25
Cells(1, 1) = Int(Rnd * samplesize) + 1
If samplesize < 2 Then Exit Sub
For x = 2 To samplesize
    Cells(x, 1) = Int(Rnd * samplesize) + 1
    Do Until Abs(Cells(x, 1) - Cells(x - 1, 1)) <= 15 And _
    WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(x, 1)), Cells(x, 1)) = 1
        Cells(x, 1) = Int(Rnd * samplesize) + 1
    Loop
Next x

End Sub
Note that this is *not* foolproof. Imagine your sample is of 100 numbers and the first element is 50 and then it works up to 75. There is a chance that the numbers will never come back down to 1-10 for instance.

It should work well for your sample size, but give it a try and report back.

Bye,
Jay

[ This Message was edited by: Jay Petrulis on 2002-05-04 09:31 ]
Jay Petrulis is offline   Reply With Quote
Old May 4th, 2002, 11:33 AM   #8
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

Quote:
On 2002-05-04 09:26, Jay Petrulis wrote:
Hi,

It would probably help if I read the question thoroughly.

You want an exhaustive list with the changes bounded? That means hypergeometric sampling (sampling without replacement).

The following routine might get closer to your goal.

Code:
Sub bounded_random()
Dim x As Long, samplesize As Long
Randomize

samplesize = 25
Cells(1, 1) = Int(Rnd * samplesize) + 1
If samplesize < 2 Then Exit Sub
For x = 2 To samplesize
    Cells(x, 1) = Int(Rnd * samplesize) + 1
    Do Until Abs(Cells(x, 1) - Cells(x - 1, 1)) <= 15 And _
    WorksheetFunction.CountIf(Range(Cells(1, 1), Cells(x, 1)), Cells(x, 1)) = 1
        Cells(x, 1) = Int(Rnd * samplesize) + 1
    Loop
Next x

End Sub
Note that this is *not* foolproof. Imagine your sample is of 100 numbers and the first element is 50 and then it works up to 75. There is a chance that the numbers will never come back down to 1-10 for instance.

It should work well for your sample size, but give it a try and report back.

Bye,
Jay
Sorry,but that's not what I mean.
I need 3 different numbers on a row
They may not have a difference of more then 15 between each number.
Ex: 1 7 12
18 20 25
1 17 22 = not correct
Thanks for help.

[ This Message was edited by: Jay Petrulis on 2002-05-04 09:31 ]
verluc is offline   Reply With Quote
Old May 4th, 2002, 11:47 PM   #9
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

The nerve of some people. They post a question and expect *that specific question* to be answered! OK, so I forgot how to read twice.

This works for three columns, but I am trying to generalize this for more. That requires a recursive routine which I am unable to do as of yet.

Anyway, try the following. Warning: this is very slow, and it clears the active sheet, so please don't use it on a filled sheet without changing it.

Code:
Sub test()
Dim a As Integer, b As Integer, c As Integer
Dim lngCounter As Long, lastrow As Long
Dim population As Integer, sample As Integer, maxdiff As Integer
Dim CalcSetting, x As Long

population = 25
sample = 3
maxdiff = 15

If (population ^ sample) > 65536 Then
    MsgBox "Too many to handle"
    Exit Sub
End If

CalcSetting = Application.Calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

With ActiveSheet
    .Cells.ClearContents
    For a = 1 To population
        For b = 1 To population
            For c = 1 To population
                .Cells(lngCounter + 1, 1) = a
                .Cells(lngCounter + 1, 2) = b
                .Cells(lngCounter + 1, 3) = c
                .Cells(lngCounter + 1, sample + 1).FormulaArray = _
                "=SUM(IF(ABS(RC[-" & sample - 1 & "]:RC[-1]-RC[-" _
                & sample & "]:RC[-2])<=" & maxdiff & ",1))"
                lngCounter = lngCounter + 1
            Next c
        Next b
    Next a
    If (population ^ sample) < 65536 Then
        .Rows(1).Insert
        .Cells(1, sample + 1) = "temp"
        .Cells(1, sample + 1).AutoFilter Field:=sample + 1, Criteria1:="<>" & sample - 1
        .Cells.SpecialCells(xlCellTypeVisible).Delete shift:=xlUp
        .Columns(sample + 1).ClearContents
    Else
        For x = 65536 To 1 Step -1
            If .Cells(x, sample + 1) <> sample - 1 Then Rows(x).Delete
        Next x
    End If
End With
Application.Calculation = CalcSetting
End Sub
Will try to get the recursive routine working.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old May 5th, 2002, 02:53 AM   #10
verluc
Board Regular
 
Join Date: Mar 2002
Posts: 1,290
Default

Quote:
On 2002-05-04 22:47, Jay Petrulis wrote:
The nerve of some people. They post a question and expect *that specific question* to be answered! OK, so I forgot how to read twice.

This works for three columns, but I am trying to generalize this for more. That requires a recursive routine which I am unable to do as of yet.

Anyway, try the following. Warning: this is very slow, and it clears the active sheet, so please don't use it on a filled sheet without changing it.

Code:
Sub test()
Dim a As Integer, b As Integer, c As Integer
Dim lngCounter As Long, lastrow As Long
Dim population As Integer, sample As Integer, maxdiff As Integer
Dim CalcSetting, x As Long

population = 25
sample = 3
maxdiff = 15

If (population ^ sample) > 65536 Then
    MsgBox "Too many to handle"
    Exit Sub
End If

CalcSetting = Application.Calculation
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

With ActiveSheet
    .Cells.ClearContents
    For a = 1 To population
        For b = 1 To population
            For c = 1 To population
                .Cells(lngCounter + 1, 1) = a
                .Cells(lngCounter + 1, 2) = b
                .Cells(lngCounter + 1, 3) = c
                .Cells(lngCounter + 1, sample + 1).FormulaArray = _
                "=SUM(IF(ABS(RC[-" & sample - 1 & "]:RC[-1]-RC[-" _
                & sample & "]:RC[-2])<=" & maxdiff & ",1))"
                lngCounter = lngCounter + 1
            Next c
        Next b
    Next a
    If (population ^ sample) < 65536 Then
        .Rows(1).Insert
        .Cells(1, sample + 1) = "temp"
        .Cells(1, sample + 1).AutoFilter Field:=sample + 1, Criteria1:="<>" & sample - 1
        .Cells.SpecialCells(xlCellTypeVisible).Delete shift:=xlUp
        .Columns(sample + 1).ClearContents
    Else
        For x = 65536 To 1 Step -1
            If .Cells(x, sample + 1) <> sample - 1 Then Rows(x).Delete
        Next x
    End If
End With
Application.Calculation = CalcSetting
End Sub
Will try to get the recursive routine working.

Bye,
Jay
verluc 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 05:18 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