![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
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 |
|
|
|
|
|
#2 | |
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
Quote:
1 2 3 CORRECT 1 2 18 INCORRECT |
|
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
|
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 |
|
|
|
|
|
#4 | |
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
Quote:
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 |
|
|
|
|
|
|
#5 | ||
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
Quote:
Thanks |
||
|
|
|
|
|
#6 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
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
Bye, Jay |
|
|
|
|
|
#7 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
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
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 ] |
|
|
|
|
|
#8 | |
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
Quote:
|
|
|
|
|
|
|
#9 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
|
The nerve of some people. They post a question and expect *that specific question* to be answered!
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
Bye, Jay |
|
|
|
|
|
#10 | |
|
Board Regular
Join Date: Mar 2002
Posts: 1,290
|
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|