简体   繁体   中英

Export misspelled words

After searching I came across this to highlight misspelled words in cells in excel, however I want to copy any misspelled word found to a different worksheet and list them all out in a column. I understand most of existing code below but struggle with how it identifies the misspelled word in the cell string and how you can then copy that to another worksheet column A. Any guidance welcomed not matter how small. thanks.

Dim cel As Range, CellLen As Long, CurChr As Long, TheString As String
    For Each cel In Selection
        For CurChr = 1 To Len(cel.Value)
            If Asc(Mid(cel.Value, CurChr, 1)) = 32 Then
                If InStr(CurChr + 1, cel.Value, " ") = 0 Then
                    TheString = Mid(cel.Value, CurChr + 1, Len(cel.Value) - CurChr)
                Else
                   TheString = Mid(cel.Value, CurChr + 1, InStr(CurChr + 1, cel.Value, " ") - CurChr)
                End If
                If Not Application.CheckSpelling(Word:=TheString) Then
                    cel.Characters(CurChr + 1, Len(TheString)).Font.Color = RGB(255, 0, 0)
                Else
                    cel.Characters(CurChr + 1, Len(TheString)).Font.Color = RGB(0, 0, 0)
                End If
                TheString = ""
            End If
        Next CurChr
    Next cel

I might do something like this:

Dim cel As Range, theString As String
Dim totalString() As String, wsPaste As Worksheet, rowNumber As Long, arrayIndex As Long
Dim s as string

Set wsPaste = Worksheets("insertYourWorksheetNameHere")

'set the "start row" for where to paste words on wsPaste
rowNumber = 1

For Each cel In Selection.Cells

    'If we find spaces, make an array with all the "stuff" between the spaces
    'otherwise, just make an array with a single value in it of the whole cell's value
    s = cel.value2
    If InStr(1, s, " ") > 0 Then
        totalString = Split(s, " ")
    Else
        ReDim totalString(0 To 0)
        totalString(0) = s
    End If

    'loop through our array, checking each "word" using the Application.CheckSpelling function
    For arrayIndex = 0 To UBound(totalString)
        theString = totalString(arrayIndex)
        If Not Application.CheckSpelling(Word:=theString) Then
            'We found a misspelled word! Set the next value on our wsPaste sheet
            'to the misspelled word, and increment the row counter
            wsPaste.Cells(rowNumber, 1).Value = theString
            rowNumber = rowNumber + 1
        End If
    Next

    'probably not necessary, but the details of which are beyond the scope of this question
    Erase totalString

Next

I tried to comment as much as possible.

Anyway, so, the old code you were using had all this gross string manipulation, which was necessary for what it was trying to do. Basically, lines 5-9 check for spaces (the Ascii value for a space, " ", is 32, btw, which is what that was checking for), and that's a decent way to get the position of the word in the string, so that you can change the font of that specific word in the cell value. However, if you don't need to know where in the string each word is, you just want to move the misspelled words somewhere else, we can use the Split function.

Basically, the Split function is making an array from a string, splitting it along the " ", and storing each "word" in a spot in the array. If there are NO spaces, we just assign an array with the single value of the cell. Then, we loop through the array, using the Application.CheckSpelling function that is built in. When we find a misspelled word, we paste it into the wsPaste sheet, and move on!

Let me know if you have questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM