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.
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
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 24 years 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
-
My Simple Word 2010 Macro Is Not Working (Awaiting moderation)
by
mbennett555
4 hours, 6 minutes ago -
Office gets current release
by
Susan Bradley
8 hours, 50 minutes ago -
FBI: Still Using One of These Old Routers? It’s Vulnerable to Hackers
by
Alex5723
1 day, 1 hour ago -
Windows AI Local Only no NPU required!
by
RetiredGeek
9 hours, 53 minutes ago -
Stop the OneDrive defaults
by
CWBillow
1 day, 2 hours ago -
Windows 11 Insider Preview build 27868 released to Canary
by
joep517
1 day, 12 hours ago -
X Suspends Encrypted DMs
by
Alex5723
1 day, 14 hours ago -
WSJ : My Robot and Me AI generated movie
by
Alex5723
1 day, 14 hours ago -
Botnet hacks 9,000+ ASUS routers to add persistent SSH backdoor
by
Alex5723
1 day, 15 hours ago -
OpenAI model sabotages shutdown code
by
Cybertooth
1 day, 15 hours ago -
Backup and access old e-mails after company e-mail address is terminated
by
M W Leijendekker
1 day, 4 hours ago -
Enabling Secureboot
by
ITguy
1 day, 11 hours ago -
Windows hosting exposes additional bugs
by
Susan Bradley
1 day, 23 hours ago -
No more rounded corners??
by
CWBillow
1 day, 19 hours ago -
Android 15 and IPV6
by
Win7and10
1 day, 9 hours ago -
KB5058405 might fail to install with recovery error 0xc0000098 in ACPI.sys
by
Susan Bradley
2 days, 12 hours ago -
T-Mobile’s T-Life App has a “Screen Recording Tool” Turned on
by
Alex5723
2 days, 14 hours ago -
Windows 11 Insider Preview Build 26100.4202 (24H2) released to Release Preview
by
joep517
2 days, 9 hours ago -
Windows Update orchestration platform to update all software
by
Alex5723
2 days, 22 hours ago -
May preview updates
by
Susan Bradley
2 days, 9 hours ago -
Microsoft releases KB5061977 Windows 11 24H2, Server 2025 emergency out of band
by
Alex5723
2 days, 1 hour ago -
Just got this pop-up page while browsing
by
Alex5723
2 days, 14 hours ago -
KB5058379 / KB 5061768 Failures
by
crown
2 days, 11 hours ago -
Windows 10 23H2 Good to Update to ?
by
jkitc
1 day, 13 hours ago -
At last – installation of 24H2
by
Botswana12
3 days, 13 hours ago -
MS-DEFCON 4: As good as it gets
by
Susan Bradley
11 hours, 22 minutes ago -
RyTuneX optimize Windows 10/11 tool
by
Alex5723
4 days, 1 hour ago -
Can I just update from Win11 22H2 to 23H2?
by
Dave Easley
2 days ago -
Limited account permission error related to Windows Update
by
gtd12345
4 days, 15 hours ago -
Another test post
by
gtd12345
4 days, 15 hours 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.