拼写检查单元格中的单词拼写错误并将其颜色更改为红色

我正在做一个自动化,在那里我需要拼写检查一个单元格内的一个段落,并突出显示红色的错误单词,这个拼写检查我需要做的列将有dynamic范围。

我正在使用下面的代码

Sub spellcheck() Dim intOffsetCol As Integer Dim intChrCnt As Integer Dim varTempString As Variant intOffsetCol = 1 For intChrCnt = 1 To Trim(Len(ActiveCell.Value)) Step 1 'Left to right 'A space character is represented by an ASCII code of 32. If Asc(Mid(ActiveCell.Value, intChrCnt, 1)) <> 32 Then varTempString = varTempString & Mid(ActiveCell.Value, intChrCnt, 1) Else If Not Application.CheckSpelling(Word:=varTempString) Then varTempString.Interior.ColorIndex = 52 varTempString = "" intOffsetCol = intOffsetCol + 1 End If End If Next intChrCnt If varTempString <> "" Then If Not Application.CheckSpelling(Word:=varTempString) Then varTempString.Interior.ColorIndex = vbRed End If End Sub 

而且我得到varTempString.Interior.ColorIndex = 52上的“运行时错误424”

为什么我在这一行上遇到错误?

varTempString被用作Stringvariables,你的代码正试图改变不可能的单元格内部。

突出显示特定文本的解决scheme是:

 Sub SpellCheckUpdate() 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 End Sub 

这是更简单的代码来突出拼写单词的完整单元格:

 Sub ColorMispelledCells() For Each cl In ActiveSheet.UsedRange If Not Application.CheckSpelling(Word:=cl.Text) Then _ cl.Interior.ColorIndex = 28 Next cl End Sub