导出拼写错误的单词

Export misspelled words

在搜索之后,我发现这个可以突出显示 excel 中单元格中拼写错误的单词,但是我想将发现的任何拼写错误的单词复制到不同的工作表中,并将它们全部列在一个列中。我理解下面的大部分现有代码,但对它如何识别单元格字符串中拼写错误的单词以及如何将其复制到另一个工作表 A 列感到困惑。无论多小,都欢迎任何指导。谢谢。

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

我可能会这样做:

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

我尽量多发表评论。

无论如何,您使用的旧代码具有所有这些粗略的字符串操作,这对于它试图做的事情是必要的。基本上,第 5-9 行检查 spaces(space、" " 的 Ascii 值是 32,顺便说一句,这是检查的内容),这是一个不错的方法单词在字符串中的位置,以便您可以更改单元格值中该特定单词的字体。但是,如果您不需要知道每个单词在字符串中的位置,而只想将拼写错误的单词移动到其他地方,我们可以使用 Split 函数。

基本上,Split 函数是从一个字符串创建一个数组,沿着“”拆分它,并将每个 "word" 存储在数组中的一个位置。如果没有 spaces,我们只需分配一个包含单元格单个值的数组。然后,我们使用内置的 Application.CheckSpelling 函数遍历数组。当我们找到拼写错误的单词时,我们将其粘贴到 wsPaste sheet 中,然后继续!

如果您有任何问题,请告诉我。