How can I create a procedure that will create all the combinations of the numbers 0 to 9? E.g. 0123456789, 1023456789,.. I will store them one by one in a column array. Cheers, Andy.
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Combinations
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Combinations
- This topic has 6 replies, 4 voices, and was last updated 23 years, 11 months ago.
AuthorTopicWSandrewgibsonsw
AskWoody LoungerJune 1, 2001 at 9:49 am #356576Viewing 0 reply threadsAuthorReplies-
H. Legare Coleman
AskWoody PlusJune 1, 2001 at 1:31 pm #528037Beware that the following is going to run for a LONG time.
Public Sub AllNums() Dim I0 As Integer, I1 As Integer, I2 As Integer, I3 As Integer, I4 As Integer Dim I5 As Integer, I6 As Integer, I7 As Integer, I8 As Integer, I9 As Integer Dim strNumber As String For I0 = 0 To 9 For I1 = 0 To 9 For I2 = 0 To 9 For I3 = 0 To 9 For I4 = 0 To 9 For I5 = 0 To 9 For I6 = 0 To 9 For I7 = 0 To 9 For I8 = 0 To 9 For I9 = 0 To 9 strNumber = CStr(I0) & CStr(I1) & CStr(I2) & CStr(I3) strNumber = strNumber & CStr(I4) & CStr(I5) & CStr(I6) strNumber = strNumber & CStr(I7) & CStr(I8) & CStr(I9) Next I9 Next I8 Next I7 Next I6 Next I5 Next I4 Next I3 Next I2 Next I1 Next I0 End Sub
-
WSgwhitfield
AskWoody LoungerJune 1, 2001 at 9:09 pm #528092Leagre,
Would that code just give every number between 1 and 9999999999, rather than every combination of 10 digits (ie, with no repeating digits)- which is the way I see the question (I may be wrong though).
Otherwise, the following might do the same:
For i = 1 To 9999999999 strNumber = Format(i, "0000000000") Next
Otherwise, the code might me changed to:
For I0 = 0 To 9 For I1 = 0 To 9 If I1 I0 Then For I2 = 0 To 9 If I2 I1 And I2 I0 Then
etc.
I’m sure thought theres a better way.
-
H. Legare Coleman
AskWoody PlusJune 1, 2001 at 11:19 pm #528098 -
WSandrewgibsonsw
AskWoody Lounger -
WSandrewgibsonsw
AskWoody LoungerJune 3, 2001 at 2:08 pm #528154The following code seems to be doing what I want. It still seems slow though??
Sub Combs()
Dim strNumber As String
Dim varCombs As Variant
Dim intCounter As IntegerFor varCombs = 123456789 To 9876543210#
strNumber = CStr(Format(varCombs, “0000000000”))
For intCounter = 1 To 9
If InStr(Right(strNumber, 10 – intCounter), Mid(strNumber, intCounter, 1)) > 0 Then
GoTo SkipNumber
End If
Next intCounter
Debug.Print strNumber
SkipNumber:
Next varCombs
End SubThis code generates the entire sequence of numbers, then performs string comparisons to if the generated number contains duplicates. Any other ideas?
-
WSIanSaunders
AskWoody LoungerJune 4, 2001 at 10:41 pm #528265The following code generates all the permutations directly. It could probably be improved by more expert VBA’ers.
The last line of output for n=9 on a PIII at 800Mhz or so was:
Total = 362880 in 183.1543 seconds. (1981.28029858704/sec.)
so n=10 would take 10 times this – about 30 minutes. 40 seconds of this is generating the permutations and the other 29m20s is printing them out!
Ian.
Sub Caller() Dim iPerm() As Integer Dim k As Long Dim strPrint As String Dim Start As Single n = InputBox("Number of items") Start = Timer ReDim iPerm(1 To n) For i = 1 To n iPerm(i) = i Next i k = 0 Do While iPerm(1) 0 k = k + 1 strPrint = "" For i = 1 To n strPrint = strPrint & (iPerm(i) - 1) Next i Debug.Print strPrint & " " & k iPerm = NextPerm(iPerm) Loop Debug.Print ("Total = " & k & " in " & (Timer - Start) & "seconds. (" & k / (Timer - Start) & "/sec.)") End Sub ' ' Function NextPerm(iPerm() As Integer) As Variant ' iPerm() contains a permutation of 1 2 3 ... n ' NextPerm returns the next permutation of 1 2 ... n after iPerm ' or sets iPerm(1) to 0 if iPerm is the last permutation ' n n-1 ... 1 Dim SubSet() As Integer Dim n As Integer n = UBound(iPerm) If iPerm(1) < n Then 'Shift n left by one place For j = 2 To n If iPerm(j) = n Then iPerm(j) = iPerm(j - 1) iPerm(j - 1) = n NextPerm = iPerm Exit Function End If Next j Else If n = 2 Then 'Done iPerm(1) = 0 NextPerm = iPerm Exit Function End If ' Get the next permutation of 1 ... (n-1) ' and add n at the end ReDim SubSet(1 To (n - 1)) For j = 1 To n - 1 SubSet(j) = iPerm(j + 1) Next j SubSet = NextPerm(SubSet) If SubSet(1) = 0 Then iPerm(1) = 0 NextPerm = iPerm Exit Function End If For j = 1 To n - 1 iPerm(j) = SubSet(j) Next j iPerm(n) = n NextPerm = iPerm End If End Function
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Downloads folder location
by
CWBillow
4 hours, 54 minutes ago -
Remove a User from Login screen
by
CWBillow
7 hours, 26 minutes ago -
TikTok fined €530 million for sending European user data to China
by
Nibbled To Death By Ducks
8 hours, 35 minutes ago -
Microsoft Speech Recognition Service Error Code 1002
by
stanhutchings
14 hours, 40 minutes ago -
Is it a bug or is it expected?
by
Susan Bradley
40 minutes ago -
Image for Windows TBwinRE image not enough space on target location
by
bobolink
11 hours, 13 minutes ago -
Start menu jump lists for some apps might not work as expected on Windows 10
by
Susan Bradley
19 hours, 13 minutes ago -
Malicious Go Modules disk-wiping malware
by
Alex5723
8 hours, 49 minutes ago -
Multiple Partitions?
by
CWBillow
9 hours, 29 minutes ago -
World Passkey Day 2025
by
Alex5723
1 day, 2 hours ago -
Add serial device in Windows 11
by
Theodore Dawson
1 day, 18 hours ago -
Windows 11 users reportedly losing data due forced BitLocker encryption
by
Alex5723
1 hour, 30 minutes ago -
Cached credentials is not a new bug
by
Susan Bradley
1 day, 22 hours ago -
Win11 24H4 Slow!
by
Bob Bible
1 day, 22 hours ago -
Microsoft hiking XBox prices starting today due to Trump’s tariffs
by
Alex5723
1 day, 19 hours ago -
Asus adds “movement sensor” to their Graphics cards
by
n0ads
2 days ago -
‘Minority Report’ coming to NYC
by
Alex5723
1 day, 21 hours ago -
Apple notifies new victims of spyware attacks across the world
by
Alex5723
2 days, 9 hours ago -
Tracking content block list GONE in Firefox 138
by
Bob99
2 days, 9 hours ago -
How do I migrate Password Managers
by
Rush2112
1 day, 16 hours ago -
Orb : how fast is my Internet connection
by
Alex5723
1 day, 18 hours ago -
Solid color background slows Windows 7 login
by
Alex5723
2 days, 21 hours ago -
Windows 11, version 24H2 might not download via Windows Server Updates Services
by
Alex5723
2 days, 19 hours ago -
Security fixes for Firefox
by
Susan Bradley
1 day, 20 hours ago -
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
3 days, 8 hours ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
3 days, 17 hours ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
3 days, 8 hours ago -
Return of the brain dead FF sidebar
by
EricB
2 days, 19 hours ago -
Windows Settings Managed by your Organization
by
WSDavidO61
1 day, 22 hours ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
1 day ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.