在 VBA 中对 Excel 工作表进行拼写检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12057456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spell check an Excel sheet in VBA
提问by STANGMMX
I have scoured the Internet and I have found a handful of possible solutions to this issue, but I wanted to ask here as well.
我在互联网上搜索并找到了一些解决此问题的可能方法,但我也想在这里提问。
The goal is to click a button, and spell check an entire sheet.
目标是单击一个按钮,然后对整个工作表进行拼写检查。
Here's some code
这是一些代码
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
Also, I found this:
另外,我发现了这个:
Sub SpellCheck()
Dim Checkword As String, Result As Boolean
Checkword = Selection.Value
Result = Application.CheckSpelling(Checkword)
Selection.Offset(0, 1) = Result
End Sub
Any ideas? Neither is working for me. Thanks!
有任何想法吗?两者都不适合我。谢谢!
回答by CaptElmo
You can check the whole workbook by doing something like:
您可以通过执行以下操作来检查整个工作簿:
Sub SpellCheck()
For Each sh In Worksheets
Sheets(sh.Name).Cells.CheckSpelling
Next
End Sub
this will cycle through each sheet in the entire book and run a spellcheck on each one. What I can't figure out yet is how to make the spell checker actually move to the position of the spelling error. So, with the above you just get a list of spelling errors with no context with which to asses them.
这将循环浏览整本书中的每一页,并对每页进行拼写检查。我还想不通的是如何让拼写检查器实际移动到拼写错误的位置。因此,通过上面的内容,您只会得到一个拼写错误列表,而没有可以评估它们的上下文。
回答by Sathish Kothandam
This code will work on selected cells .This will highlight if any spell mistakes in a cell
此代码适用于选定的单元格。这将突出显示单元格中是否有拼写错误
Dim Myrange As Range
Selection.SpecialCells(xlVisible).Select
For Each Myrange In Selection
If Application.CheckSpelling(word:=Myrange.Value) = False Then
Myrange.Font.Color = vbRed
End If
Next
回答by Tim Cox
This uses a code snippet from a previous answer to restrict the area used for spellchecking to a specific region. Something I needed to do in a small project. This give the full functionallity of the spellchecker with errors shown in context. The rowNumberis calculated elsewhere.The selection range can be fully controlled elsewhere in your code to suit your particular need. Thought this might help others searching this posting.
这使用来自先前答案的代码片段将用于拼写检查的区域限制为特定区域。我需要在一个小项目中做的事情。这提供了拼写检查器的完整功能,并在上下文中显示错误。该ROWNUMBER计算elsewhere.The选择范围可以完全在你的代码,以满足您的特定需求的其他地方控制。认为这可能有助于其他人搜索此帖子。
With Sheets("Sheet1")
slic = CStr(rowNumber)
.Range("AL3:AN" & slic).Select
Application.CommandBars("Tools").Controls("Spelling...").Execute
End With
Thanks to previous posters this solved a problem form me. I am most grateful.
感谢以前的海报,这解决了我的问题。我非常感激。
回答by STANGMMX
I noticed I just had a typo in my code.
我注意到我的代码中有一个错字。
Below works:
以下作品:
Sub spellCheck()
Sheet1.Cells.CheckSpelling
End Sub
But, if anyone knows how to do the entire workbook, I'd be interested in that. Thanks.
但是,如果有人知道如何完成整个工作簿,我会对此感兴趣。谢谢。
回答by CaptElmo
OK, so you can use the following command to invoke the toolbar's spellchecker which does move you to the position of the spelling error as long as you have screen updating enabled at the time.
好的,所以您可以使用以下命令调用工具栏的拼写检查器,只要您当时启用了屏幕更新,它就会将您移动到拼写错误的位置。
Application.CommandBars("Tools").Controls("Spelling...").Execute
You can use this command embedded in the loop above to loop through the sheets in the workbook and invoke this command on each new sheet.
您可以使用嵌入在上述循环中的此命令来循环遍历工作簿中的工作表并在每个新工作表上调用此命令。
Cap
帽