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, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
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, 7 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
-
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
9 hours, 41 minutes ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
5 hours, 43 minutes ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
12 hours, 7 minutes ago -
Return of the brain dead FF sidebar
by
EricB
2 hours, 44 minutes ago -
windows settings managed by your organization
by
WSDavidO61
6 hours, 49 minutes ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
7 hours, 2 minutes ago -
The local account tax
by
Susan Bradley
2 hours, 52 minutes ago -
Recall is back with KB5055627(OS Build 26100.3915) Preview
by
Alex5723
18 hours, 44 minutes ago -
Digital TV Antenna Recommendation
by
Win7and10
11 hours, 16 minutes ago -
Server 2019 Domain Controllers broken by updates
by
MP Support
1 day, 6 hours ago -
Google won’t remove 3rd party cookies in Chrome as promised
by
Alex5723
1 day, 8 hours ago -
Microsoft Manager Says macOS Is Better Than Windows 11
by
Alex5723
1 day, 11 hours ago -
Outlook (NEW) Getting really Pushy
by
RetiredGeek
13 hours, 53 minutes ago -
Steps to take before updating to 24H2
by
Susan Bradley
4 hours, 42 minutes ago -
Which Web browser is the most secure for 2025?
by
B. Livingston
18 hours, 19 minutes ago -
Replacing Skype
by
Peter Deegan
6 hours, 52 minutes ago -
FileOptimizer — Over 90 tools working together to squish your files
by
Deanna McElveen
1 day, 5 hours ago -
Excel Macro — ask for filename to be saved
by
nhsj
2 hours, 49 minutes ago -
Trying to backup Win 10 computer to iCloud
by
SheltieMom
6 hours, 39 minutes ago -
Windows 11 Insider Preview build 26200.5570 released to DEV
by
joep517
3 days, 11 hours ago -
Windows 11 Insider Preview build 26120.3941 (24H2) released to BETA
by
joep517
3 days, 13 hours ago -
Windows 11 Insider Preview Build 22635.5305 (23H2) released to BETA
by
joep517
3 days, 13 hours ago -
No April cumulative update for Win 11 23H2?
by
Peobody
2 days, 1 hour ago -
AugLoop.All (TEST Augmentation Loop MSIT)
by
LarryK
3 days, 13 hours ago -
Boot Sequence for Dell Optiplex 7070 Tower
by
Serge Carniol
4 days, 4 hours ago -
OTT Upgrade Windows 11 to 24H2 on Unsupported Hardware
by
bbearren
4 days, 8 hours ago -
Inetpub can be tricked
by
Susan Bradley
2 days, 15 hours ago -
How merge Outlook 2016 .pst file w/into newly created Outlook 2024 install .pst?
by
Tex265
3 days, 2 hours ago -
FBI 2024 Internet Crime Report
by
Alex5723
4 days, 12 hours ago -
Perplexity CEO says its browser will track everything users do online
by
Alex5723
1 day, 21 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.