erik.van.geit
MrExcel MVP
- Joined
- Feb 1, 2003
- Messages
- 17,832
Hi, guys and galls,
this is one for the gurus
A1 has some characters
this code will generate all possible words, that can be made using all characters
system
permutate and checkspelling: if OK then write to column B
example
A1: iftrs
results: first frits rifts
any tricks to enhance the speed ?
thank you for reading
kind regards,
Erik
this is one for the gurus

A1 has some characters
this code will generate all possible words, that can be made using all characters
system
permutate and checkspelling: if OK then write to column B
example
A1: iftrs
results: first frits rifts
Code:
Option Explicit
Dim CurrentRow
Const col = 2
Sub correctly_spelled_permutations()
Dim InString As String
Dim CalcSet As Integer
InString = Range("A1")
If Len(InString) < 2 Then Exit Sub
With Application
.ScreenUpdating = False
CalcSet = .Calculation
.Calculation = xlCalculationManual
.EnableCancelKey = xlErrorHandler
.StatusBar = "searching valid combination"
End With
On Error GoTo skip
CurrentRow = 0
'If Len(InString) > 8 Then
'MsgBox "To many permutations!"
'Exit Sub
'Else
ActiveSheet.Columns(col).Clear
Call GetPermutation("", InString)
'End If
skip:
With Application
.Calculation = CalcSet
.ScreenUpdating = True
.StatusBar = False
End With
End Sub
Sub GetPermutation(x As String, y As String)
'The source of this algorithm is unknown
Dim i As Integer, j As Integer
j = Len(y)
With Application
If j < 2 Then
If .CheckSpelling(x & y) Then
CurrentRow = CurrentRow + 1
ActiveSheet.Cells(CurrentRow, col) = x & y
.StatusBar = "# of valid combinations: " & CurrentRow
End If
Else
For i = 1 To j
Call GetPermutation(x + Mid(y, i, 1), _
Left(y, i - 1) + Right(y, j - i))
Next
End If
End With
End Sub
any tricks to enhance the speed ?
thank you for reading

kind regards,
Erik