I need to write a vba code that would give me all the letter combinations of a word and save it into a text file (this code is optional). For, instance, the word 'aBc' would return:
aBc
acB
Bac
Bca
caB
cBa
I'm sure it's an easy code but I can't seem to figure it out.
Here's the code I have so far. It keeps giving me duplicates and not all the results.
Sub Scramble()
Dim Rand1()
a = Len(Range("a2").Value)
ReDim Rand1(a)
T = 0
Randomize
For n = 1 To a
Check1:
Rand1(n) = Int((a * Rnd(100)) + 1)
For F = 1 To T
If Rand1(n) = Rand1(F) Then GoTo Check1:
Next F
T = T + 1
Next n
For s = 2 To 20
Range("d" & s).ClearContents
n = 1
Rand1(n) = Int((a * Rnd(100)) + 1)
For n = 1 To a
Range("d" & s).Value = Range("d" & s).Value & Mid(Range("a2").Value, Rand1(n), 1)
Next n
Next s
End Sub
A recursive approach is natural. To scramble e.g.
"MATH"
you pull the letters out one at a time, scramble the remaining letters, then insert the pulled out letter in the front. Using memoization, something like this:After running this, column A looks like: