Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
x = Application.CheckSpelling(LettersString)
If x = True Then
' Add LettersString to list of acceptable words
End If
Sub AnagramOfSorts()
Dim iLenNew As Integer
Dim strOldWord As String
Dim strNewWord As String
Dim iChar As Integer
'assign original text
strOldWord = Range("A1")
Do While Len(strOldWord) > 0
Randomize
'select a character from old text to add to new text
iChar = Int(Rnd() * Len(strOldWord)) + 1
strNewWord = strNewWord + Mid(strOldWord, iChar, 1)
'recreate old text without characters already added to new text
strOldWord = Left(strOldWord, (iChar) - 1) + _
Right(strOldWord, Len(strOldWord) - iChar)
Loop
'output the "mixed" text
Range("A2") = strNewWord
End Sub
Option Explicit
Const GET_FIRST As Boolean = True
Sub test()
Dim sAnagram As String
sAnagram = "TURKEY"
Call Anagram(sAnagram, GET_FIRST)
MsgBox sAnagram
While Anagram(sAnagram)
If vbCancel = MsgBox(sAnagram + vbNewLine + vbNewLine + _
" Continue?", vbOKCancel, "Anagram") Then
Exit Sub
End If
Wend
End Sub
Function Anagram(AWord As String, _
Optional FirstOrNext As Boolean = False) As Boolean
If FirstOrNext = GET_FIRST Then
Call SortLetters(AWord)
Anagram = True
Else
Anagram = NextPermutation(AWord)
End If
End Function
Private Sub SortLetters(AWord)
Dim i As Integer
Dim j As Integer
Dim s As String
For i = 1 To Len(AWord) - 1
For j = i + 1 To Len(AWord)
If Mid(AWord, i, 1) > Mid(AWord, j, 1) Then
s = Mid(AWord, i, 1)
Mid(AWord, i, 1) = Mid(AWord, j, 1)
Mid(AWord, j, 1) = s
End If
Next j
Next i
End Sub
Private Function NextPermutation(AWord As String) As Boolean
' Algorithm: Working from right to left, find the "critical"
' position. The "critical" position is the first letter that
' is lower in sort order than the one at its right.
' Exchange the "critical" position character with the
' smallest one from the letters to the right of it that is
' larger than the "critical" letter. Then sort all letters
' to the right of the "critical" letter in ascending order.
' If no "critical" position is found, return False.
Dim i As Integer
Dim pCrit As Integer
Dim pSmallest As Integer
Dim sSmallest As String
Dim sCrit As String
Dim sLeft As String
Dim sRight As String
' Find "critical" position
For i = Len(AWord) To 2 Step -1
If Mid(AWord, i - 1, 1) < Mid(AWord, i, 1) Then
pCrit = i - 1
Exit For
End If
Next i
If pCrit = 0 Then
NextPermutation = False
Else
' Find smallest letter larger than critical
sSmallest = "z"
sCrit = Mid(AWord, pCrit, 1)
For i = pCrit + 1 To Len(AWord)
If Mid(AWord, i, 1) > sCrit Then
If Mid(AWord, i, 1) < sSmallest Then
pSmallest = i
sSmallest = Mid(AWord, pSmallest, 1)
End If
End If
Next i
' Swap with critical
Mid(AWord, pCrit, 1) = sSmallest
Mid(AWord, pSmallest, 1) = sCrit
' Sort remaining letters
sLeft = Mid(AWord, 1, pCrit)
sRight = Mid(AWord, pCrit + 1)
Call SortLetters(sRight)
AWord = sLeft + sRight
NextPermutation = True
End If
End Function