搜索后我发现这是为了突出显示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
答案 0 :(得分:1)
我可能会这样做:
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行检查空格(空格的Ascii值,"",是32,顺便说一下,这是检查的内容),这是一个不错的方法字符串中单词的位置,以便您可以更改单元格值中该特定单词的字体。但是,如果您不需要知道每个单词在字符串中的位置,您只想将拼写错误的单词移动到其他位置,我们可以使用Split函数。
基本上,Split函数是从一个字符串中生成一个数组,然后沿着" ",并存储每个"字"在阵列中的一个点。如果没有空格,我们只需指定一个具有单元格值的数组。然后,我们使用内置的Application.CheckSpelling函数遍历数组。当我们找到拼写错误的单词时,我们将其粘贴到wsPaste表中,然后继续前进!
如果您有疑问,请告诉我。