在列B中获得列A中不在字典中的单词

时间:2010-09-02 07:03:44

标签: excel excel-vba spell-checking vba

我怎样才能创建一个宏来检查A列的每个单元格,找到不在定义的字典中的单词,并在下一个单元格中写入它们(用空格分隔)。在下图中,您可以看到该宏完成后的工作表示例。

alt text

完整的想法是从数据库中获取(varchar)列并使用excel进行拼写检查。下一步是向负责用户发送一封电子邮件,其中包含B列中至少包含一个单词的行(当然还有列ID)。我认为我可以做其余的工作,除了这一步得到错误的话。如果您能想到拼写检查数据库列的另一个想法,如果您与我分享,我将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

您可以使用VBA使用 Application.CheckSpelling

调用内置Office词典

这是语法:

功能 CheckSpelling Word As String ,[ CustomDictionary ],[ IgnoreUppercase ])作为布尔值

...这是一个符合您要求的示例:

Option Explicit

Public Sub Checker()

Dim s           As Variant
Dim sArray      As Variant
Dim lCurrRow    As Long
Dim lStartRow   As Long
Dim lEndRow     As Long

lStartRow = 1
lEndRow = 5

Application.ScreenUpdating = False

With ThisWorkbook.Worksheets(1)
    'Clear existing data in Column B
    Call .Columns(2).ClearContents

    For lCurrRow = lStartRow To lEndRow

        'Populate an Array, splitting the song title at the spaces
        sArray = Split(.Cells(lCurrRow, 1).Text, " ")

        'Loop through each word in the Array
        For Each s In sArray
            'Spell Check against the main MS Office dictionary
            If Not Application.CheckSpelling(s) Then
                'this word is missing, output to Column B:
                .Cells(lCurrRow, 2).Value = Trim(.Cells(lCurrRow, 2).Value & " " & s)
            End If
        Next s

    Next lCurrRow
End With

Application.ScreenUpdating = True

End Sub

答案 1 :(得分:2)

不要这样做

这是一个疯狂的想法。 Excel是正确的工具。

话虽如此,它可能是可行的。

  • 首先,您必须将“句子”分成单词。这可以使用VBA到单独的工作表中完成。
  • 然后,您可以使用COUNTIF或VLOOKUP检查每个单词是否存在。例如,如果您有一个名为dictionary的工作表,其中包含按字母顺序排列的所有有效字词(在第一列中),则对=IF(VLOOKUP(B2;dictionary!A:A;1)<>B2;B2;"")中的字词最有效的方法是B2
  • 然后你可以连接每个句子的结果单元格,或者只找到第一个单元格(再次使用VLOOKUP,现在使用额外的参数FALSE)。

但是......忘了吧!