excel - VBA 拼写检查,突出显示拼写错误的单词的单元格

标签 excel vba

如何更改我的 VBA 代码以突出显示有拼写错误的单元格?

尝试添加 Application.ScreenUpdating = True但没有成功。

Sub SpellCheckSheet()
Application.ScreenUpdating = True
    Dim xRg As Range
    Call unprotect_sheet
    Set xRg = ActiveSheet.Range("A:C")
    xRg.CheckSpelling
    Call protect_sheet
End Sub

谢谢。

尝试这个,但是当我取消拼写检查器时,它不会停止循环。

Sub SpellCheckSheet()

Dim cell As Range
With ActiveSheet
    .Unprotect ("123")
        For Each cell In Range("A:C")
            .CheckSpelling
        Next cell
    .Protect ("123")
End With
End Sub

是否有一种方法可以识别 A:C 列中未 protected 单元格,然后针对每个单元格激活/选择该单元格(以便您可以在屏幕上看到它),然后激活该特定单元格上的拼写检查?

最佳答案

循环遍历单元格并对每个单元格进行拼写检查。

Sub SpellCheckSheet()
    Dim cell As Range
    With ActiveSheet
        .Unprotect "123"
            For Each cell In Range("A:C")
                If Not Application.checkSpelling(cell) Then
                    'if spellcheck was not ok color it red
                    cell.Interior.Pattern = xlSolid
                    cell.Interior.Color = 255
                End If 
            Next cell
        .Protect "123"
    End With
End Sub

请注意For Each cell In Range("A:C")循环遍历所有单元格,直到工作表的最后,由于所有单元格都是空的,这可能需要很长时间。

找到最后使用的行

Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

并将其限制为仅数据

For Each cell In Range("A:C").Resize(RowSize:=LastRow)

针对长度超过 255 个字符的单元格的解决方法

按空格将长单元格拆分为单词并对每个单词进行拼写检查。

Sub SpellCheckSheet()
    Dim LastRow As Long
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Dim cell As Range
    With ActiveSheet
        For Each cell In Range("A:C").Resize(RowSize:=LastRow)
            Dim SpelledCorrectly As Boolean
            
            If Len(cell) > 0 Then  ' ignore empty cells
                If Len(cell) < 255 Then  ' spellcheck short texts at once
                    SpelledCorrectly = Application.CheckSpelling(cell)
                Else  ' split long texts into words and spellcheck each word
                    Dim Words() As String
                    Words = Split(cell)
                    
                    Dim Word As Variant
                    For Each Word In Words  ' loop through all words and spellcheck each word
                        SpelledCorrectly = Application.CheckSpelling(Word)
                        If Not SpelledCorrectly Then Exit For  ' if one word is wrong we can stop spellchecking and color the cell
                    Next Word
                End If
                
                If Not SpelledCorrectly Then
                    'if spellcheck was not ok color it red
                    cell.Interior.Pattern = xlSolid
                    cell.Interior.Color = 255
                End If
            End If
        Next cell
    End With
End Sub

关于excel - VBA 拼写检查,突出显示拼写错误的单词的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71797738/

相关文章:

excel - 如何找到在excel中选择的列的总宽度?

excel - 从晨星提取特定的表格单元格,然后循环到下一个晨星页面

vba - Word VBA Selection.TypeText 和 Selection.InsertParagraph 以错误的顺序插入文本

excel - 如果参数为设置为单元格范围的变体数组,则 CallByName 会出现意外结果

java - Java REST 客户端接收和保存 excel 响应(字节数组)时出错

vba - 用 Excel VBA 中的函数替换单元格区域

string - 在 Excel 的 VBA 中,当字符串包含 '&' 时如何操作页眉/页脚

vba - 使用 onAction 和参数在运行时在 word vba 中构建菜单

matlab - 通过matlab在excel表格中插入图表

使用表格模型禁用 Excel 数据透视表组选项