I’m looking for a way to generate, in VBA code, all possible strings that you can make, of any length, and only using each word once, from a set of n distinct words. For example if the set were {hot, cold, wet} then you can have {{hot,cold},{wet}} or {{hot, wet},cold}} etc, etc (five distinct possibilities with three words)
Does anyone have a clue how to go about this.
In fact, more ambitiously, I want a custom function procedure whose argument is, say, an array of n (unspecified number) distinct strings and whose out put is a recordset of all possible sets of strings using only the strings contained in the array’s argument.But if anyone can suggest a direction to go with this for the simpler first part I’d be very grateful!
Thanks
![]() |
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 |
-
All possible strings of n distinct words function? (Office 2000 / SP2 and VBA)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » All possible strings of n distinct words function? (Office 2000 / SP2 and VBA)
- This topic has 13 replies, 4 voices, and was last updated 23 years, 8 months ago.
AuthorTopicWSdavidhit
AskWoody LoungerSeptember 14, 2001 at 6:51 am #360336Viewing 1 reply threadAuthorReplies-
WSRuff_Hi
AskWoody LoungerSeptember 14, 2001 at 1:07 pm #542441In short, you need some form of bubble sort based routine. To illustrate, suppose you have 4 words – call them 1, 2, 3, 4. Then you can have a total of 24 different combinations – 4 x 3 x 2 x 1 = 24. If you only want a output length of 2 words then you have a total of 12 combinations (4×3).
You need to think in terms of an array of length n with each word held in one element of the array. Thus we have an array with n words (ie n elements). Each element is referenced by an index (1 thru n).
Now you want a function that takes the kth (where k = n-1 to start with) index value (in the above example ‘3’) and increases it by 1 (checking that that index value is not used in positions 1 thru k-1. If it is, then increase it again and check again. If the value of the kth index > n then go back one index value (ie k = k-1) and try to increase that value. If the function cannot increase any index value, then you should have reached the end.
Once you have successfully increased the kth index, reorder the k+1 to nth index in ascending order and output the result.
Then call this function again with the new array that you just formed. Stop calling the function when you get an array of n, n-1, n-2, …, 3, 2, 1.
Clear as mud? Consider the following example:
1234 -> into function yields 1243 (output result and use this value as the new input into the function)
1243 -> into function yields 1324
1324 -> into function yields 1342
1342 -> into function yields 1423
1423 -> into function yields 1432
…
4312 -> into function yields 4321
4321 -> function cannot increase any index -
WScharlotte
AskWoody LoungerSeptember 14, 2001 at 1:16 pm #542443[indent]
and whose out put is a recordset of all possible sets of strings using only the strings contained in the array’s argument
[/indent]You want a recordset that only contains specific string? Or do you want one that only contains records that contain specific strings? And are the string in question the ones you specified in your post? Or are you, in fact, looking for records that contain the *words* in those strings in any combination?
If you want all the records that contain some combination of the words hot, cold and wet, then try the advice already posted. If you just want records that contain at least one of those words, just use the In operator, i.e., In (“hot”,”cold”,”wet”).
-
WSdavidhit
AskWoody LoungerSeptember 14, 2001 at 10:08 pm #542553Thanks Charlotte. In my youth I could solve these problems in a snap but the neurons don’t fire as well these days. Your comments are good but I’m not sure if I’m gettting my message clear.
What I really want is this:
A customer comes into my cafe and orders a salmon foccaccia and a coffee. (verbally, no paper, no table numbers). This is entered into my computer as
order number 413 comprising order detail 1009 = ‘Salmon Foccaccia’ and Order number 413 order detail 1010 = ‘coffee’ in the linked tables ‘Orders’ and ‘Order details’.They sit down and comsume same.
Later they say to a waiter, as she passes by, “Oh, another coffee please!” But the waiter blithely enters this into the computer as order number 428 order detail number 1066 = ‘coffee.’ When they come to pay
they say “I had a foccaccia and two coffees”. They, like most people, are too indolent to say what kind of foccaccia or tell you that they ordered the second coffee separately. All I have is the information
{foccaccia, coffee, coffee} like my {hot, cold, wet}.
So I want the system to generate all possible mutually exclusive and exhaustive subsets of the set {foccaccia, coffee, coffee} where we pretend that the elements ‘coffee’ and ‘coffee’ are actually distinct for the nonce. There are in fact 5 possibilities: all three appear against one order number ( one possible choice), two appear against one order number and the third against another (3 choices) and each one appears against it’s own distinct order number (one choice again). Using the like operator the system searches through all unpaid orders (order numbers with at least one order detail not paid for) for all possible matches to each of the 5 possibilities above.
Of course it won’t be 5 if the customer says four things, it will be 18 possible arrays, etc as the numbers grow.
But let’s stick with the case of three words giving five possible corresponding arrays for now.
For each such array there will be zero or more matching arrays of order numbers. Among these possibilies is buried the true one, viz {413, 428}.
So I want my function to return the set of all arrays of all possible order number combinations which correctly match the 5 possible arrays of the words
foccaccia, coffee, coffee and THEN I wil try to rank them in desceding order of likelikhood and display them on my pay screen form.
I hope this isn’t all too much!
The operator will then cast her eye down this display and hopefully located the correct order combination
for the customer.
I need this complicated setup because it is not appropriate to the style or modus operandi of our establishment to use any of the conventional restaurant odering systems based on table numbers, portable order number tickets or anything so formal.
We are a VERY relaxed corner village shop and the customers would think we were turning into a MacDonald’s or something if we tried this.
If you have an inspired suggestion if would really help
as my experience with VBA and function procedures is still in its infancy! -
WScharlotte
AskWoody LoungerSeptember 14, 2001 at 10:20 pm #542556 -
WSdavidhit
AskWoody LoungerSeptember 14, 2001 at 11:10 pm #542557The right order can be found in theory because unless two orders or two “pairs” of orders are identical then the customer will say “I didn’t have that” or ” and I had something else”. If the orders are identical then I won’t care if they are mixed up. We’ve tried order pads and it was a monumental headache! We keep reverting to “Oh! What did you have then?”.
Sorry to make your head spin Charlotte!
I guess you wouln’t want a job in our cafe now!
I’m determined to figure somethingout but my head feels like it’s about to spin off as well.
Maybe I’ll just have a coffee and relax…
David -
WSjscher2000
AskWoody LoungerSeptember 15, 2001 at 2:03 am #542571You’re serious! I thought the toasted bagel examples were hypothetical, and it was probably more like threaded sprockets.
I like food. Food is good.
I recently had to pair an arbitrary number of strings, but only into pairs. Doing this manually for 12 different codes was too much, so I wrote this procedure:
Sub StringPairer() 'Copyright 2001 Jefferson Scher Dim strUser As String, strArray() As String strUser = InputBox("Enter items to pair, separated by commas (e.g., A,B,C)") If strUser = vbNullString Then MsgBox "Bye!" Exit Sub ElseIf InStr(1, strUser, ",") = 0 Then MsgBox "No commas found, so there's only one item and you don't need me. Bye!" Exit Sub End If strArray() = Split(strUser, ",") Dim docNewDoc As Document, i As Integer, j As Integer Set docNewDoc = Documents.Add With docNewDoc.Range For i = 0 To UBound(strArray) For j = i + 1 To UBound(strArray) .InsertAfter "(" & strArray(i) & " and " & strArray(j) & ")[SRCHCODE] " .InsertParagraphAfter Next j Next i End With End Sub
Note that this runs in Word because this list would require further editing.
Let’s take the example of A,B,C,D,E. For my purposes, I did not want to pair A with A, because I was searching a database and matching A alone was not relevant. However, you do want to pair A with A, because 2 bagels is relevant. In my code, making the j loop start at i+1 skipped the A+A, you will want to start your j loop at i. The difference is illustrated in the crude drawing I have attached: I wanted the green squares, and you want both the green and red squares.
To expand this approach to n dimensions really is asking a lot. There are people capable of thinking in recursive code who could develop a more elegant solutions, but I’d be inclined to just have a number of different loops to handle up to, say, 5 items. Beyond that you would have so many loops, well, it just seems nutty.
Well, this doesn’t even address the further complication that you want to account for all of the items in each result set. Now I have a headache.
-
WSdavidhit
AskWoody LoungerSeptember 15, 2001 at 10:42 am #542585Of course I’m serious! Now your discussion is very interesting but it’s too late for me to concentrate on it tonight but I’ll look at it in detail tomorow. Suffice to say I went back to my old university library today and dredged up some treatises on combinatorics including relevant topics on Stirling numbers and Bell numbers. All very weird but definitely related to this discussion. Back soon…
-
WSjscher2000
AskWoody Lounger -
WSdavidhit
AskWoody LoungerSeptember 16, 2001 at 1:13 am #542629Great Job! It works correctly for 3 or 4 items but something goes wrong with five items. I will check closely to see why. But the Pascal triangle for Stirling Numbers shows that there are 52 possibilities for five items and the code only generates 42. But it’s very helpful and on the right track. See attached “photocopy” for your interest.
-
WSjscher2000
AskWoody LoungerSeptember 16, 2001 at 3:25 am #542642That graphic is unreadable for me; I think it lost something in the compression. If you look at these colorful pictures, you’ll see what I missed: sets of 3 plus the 2 others as individual buckets. Maybe I should have taken more math.
http://forum.swarthmore.edu/advanced/robertd/stirling2.html
(His group of 25 contains the 10 3-1-1 solutions mixed together with the 15 2-2-1 solutions.)
-
WSdavidhit
AskWoody Lounger -
WSdavidhit
AskWoody LoungerSeptember 16, 2001 at 5:23 am #542644 -
WSdavidhit
AskWoody LoungerSeptember 16, 2001 at 12:47 am #542625
-
-
-
-
Viewing 1 reply thread -

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