PDA

View Full Version : numbers going beyond 65536...



jjjj*newguy
02-18-2008, 11:32 AM
im very new to excel,(i know im sorry) . i have a number sequence permutation program that stops at excels row limit of 65536...and dosent show any more of the permutations. ive been trying to get it to continue onto the next worksheets or workbooks without any sucess. At one point i got it showing one permutations per page? how that happened i dont know?! can someone help me with the code to get it to continue onto other sheets.... code attached
Sub TestLP()
ListPermutations 5, 3
End Sub
Sub ListPermutations(n As Long, m As Long)
' Lists the permutations of (n, m)
' Clears the active sheet, so make sure there's nothing important ...
Dim ai() As Integer
Dim i As Long
Dim iOut As Long
Cells.ClearContents
ActiveSheet.UsedRange
If n < 1 Then Exit Sub
If m > n Then Exit Sub
ReDim ai(1 To m)
' initialize to first permutation less one
For i = 1 To m - 1
ai(i) = i
Next i
ai(m) = m - 1
AppOn False
Do While Permute(ai, n, m) And iOut < Rows.Count
iOut = iOut + 1
Cells(iOut, 1).Resize(, m) = ai
Loop
AppOn
MsgBox "Listed " & Format(iOut, "#,##0") & " of " & _
Format(WorksheetFunction.Permut(n, m), "#,##0") & " permutations", _
vbInformation, "List Permutations"

End Sub
Function Permute(ByRef ai() As Integer, ByRef n As Long, ByRef m As Long) As Boolean
' Returns the next permutation of (n, m) in ai()
' Returns False if no others exist. The last permutation is {n, n-1, ... n-m+1}
Dim i As Long
Dim j As Long
Do
' increment
For i = m To 1 Step -1
ai(i) = (ai(i) Mod n) + 1
If ai(i) <> 1 Then Exit For
Next
If i = 0 Then Exit Function ' normal exit when permutations exhausted
' check for duplicates
For i = m To 1 Step -1
For j = i - 1 To 1 Step -1
If ai(i) = ai(j) Then Exit For
Next j
If j > 0 Then Exit For
Next i
Permute = i + j = 0
Loop Until Permute = True
End Function
Sub AppOn(Optional bOn As Boolean = True)
With Application
If bOn Then
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
Else
.ScreenUpdating = False
.Calculation = xlCalculationManual
End If
End With
End Sub

p45cal
02-18-2008, 12:09 PM
try changing:

Do While Permute(ai, n, m) And iOut < Rows.Count
iOut = iOut + 1
Cells(iOut, 1).Resize(, m) = ai
Loopto:

Do While Permute(ai, n, m)
iOut = iOut + 1
If iOut > Rows.Count Then
Sheets.Add
iOut = 1
End If
Cells(iOut, 1).Resize(, m) = ai
Loop
p45cal