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
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
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
-
Sometimes I wonder about these bots
by
Susan Bradley
5 hours, 51 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
11 hours, 55 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
12 hours, 55 minutes ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
12 hours, 20 minutes ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
8 hours, 50 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
15 hours, 37 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
15 hours, 39 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
4 hours, 13 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
23 hours, 46 minutes ago -
0Patch, where to begin
by
cassel23
17 hours, 48 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
1 day, 13 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
1 day, 1 hour ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 21 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
1 day, 12 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
1 day ago -
Installer program can’t read my registry
by
Peobody
6 hours, 43 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
1 day, 10 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 18 hours ago -
False error message from eMClient
by
WSSebastian42
2 days, 9 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 18 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 19 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
5 hours, 59 minutes ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 22 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
6 hours, 19 minutes ago -
Outdated Laptop
by
jdamkeene
3 days, 4 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
3 days, 9 hours ago -
Another big Microsoft layoff
by
Charlie
3 days, 9 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
11 hours, 32 minutes ago -
May 2025 updates are out
by
Susan Bradley
13 hours, 14 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 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.