Need a macro to find "present" letters of alphabet and report "missing" letters of the alphabet

Back when I was using Word 2003, someone was kind enough to write a macro for me that checked a Word document to verify which letters of the alphabet were NOT included in the document. The macro  is below. I need another macro that will report to me the opposite--which letters of the alphabet ARE present rather than which ones are missing. if anyone can help, I would be so very grateful. I have no idea where to begin, but figured this macro could be modified to make the opposite happen.

Also, if there's a way to select text in a larger document so that the macro reviews only selected text, that would be unbelievably helpful in terms of saving me time. Thanks so much in advance for any help.

Sub AtoZ()

'

' AtoZ Macro

' Checks for the presence of all letters of alphabet and reports missing ones.

'

Dim missing As String, i As Integer

missing = ""

For i = 1 To 27

Selection.Find.ClearFormatting

With Selection.Find

    If Not .Execute(FindText:=Chr(64 + i), MatchCase:=False, Wrap:=wdFindContinue, Forward:=True) = True Then

        missing = missing & Chr(64 + i) & ","

    End If

End With

Next i

If Len(missing) > 1 Then

    missing = "The following letters are not included in the text: " & Left(missing, Len(missing) - 2) & "."

Else

    missing = "All of the letters of the alphabet are included in the text."

End If

MsgBox missing

End Sub

Answer
Answer

Arlene, just so you know how Hans knew there was a breakpoint: The big red dot in the left margin (partly covered by the yellow arrow) indicates a breakpoint -- that is, a place where the macro will stop when VBA attempts to execute that line. You can set and remove a breakpoint by putting the cursor on the line and pressing F9, or by clicking in the gray left margin next to the line (that's probably how you got it in the first place).

Breakpoints are useful for debugging macros. Let's say you have a macro that does a lot of work, and somewhere near the end something is going wrong. You can set a breakpoint at the beginning of the part that probably contains the error, and then start the macro and let it run. When it gets to the breakpoint, VBA will stop and display the macro code. Then you can repeatedly press F8 to execute one line at a time (and there are a few other commands on the Debug menu that also control what executes), and look at the values of the variables to figure out what's happening.

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

Try this version:

Sub AtoZ()
    '
    ' AtoZ Macro
    ' Checks for the presence of all letters of alphabet and reports present ones.
    '
    Dim present As String, i As Integer, strFind As String, choice As Integer, part As String
    choice = MsgBox("Click Yes to search the entire document, No to search the selection only.", _
        vbYesNo + vbQuestion)
    If choice = vbYes Then
        strFind = ActiveDocument.Content.Text
        part = "document"
    Else
        strFind = Selection.Text
        part = "selection"
    End If
    present = ""
    For i = 1 To 26
        If InStr(1, strFind, Chr(64 + i), vbTextCompare) Then
            present = present & Chr(64 + i) & ","
        End If
    Next i
    If Len(present) = 52 Then
        present = "All of the letters of the alphabet are included in the " & part & "."
    Else
        present = "The following letters are included in the " & part & ": " & _
            Left(present, Len(present) - 1) & "."
    End If
    MsgBox present, vbInformation
End Sub

---
Best wishes, HansV
https://www.eileenslounge.com

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 484 Applies to: