2017-10-10 65 views
-1

嗨我需要检查拼写和缩写列中的所有数据。列拼写检查VBA Excel

这里是我的代码:

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

无论如何,我可以改变这使其成为基于列的检查,而不是细胞,而不是hightlight细胞,而是添加注释到下一列,这个词是拼写错误或缩写?

+0

你是什么触发直接输入内容后?或者当按下按钮(或类似的东西)时 – FunThomas

+0

当按下按钮时,触发器 – Gorhell

+0

你是什么意思“在下一列添加注释”?在cl.Offset(Columns:= 1)中写入* something * '?不清楚你在找什么,顺便说一下'If ... Then'语句具有危险的误导性 - 在'If ... End If'块中写多行'If'语句,并且内联条件一条线上的onals。这里的行延续和块缩进是*乞求*后面会引入的错误。 –

回答

1

您可以将循环更改为for循环以遍历单个列。您将需要该消息应该是什么做的更多的细节,如果它的拼写,缩写等

Dim i as Long, j as Long, LR as Long 
j = 1 'Setting this up for Column A, aka Column 1 
LR = Cells(Rows.Count, j).End(xlUp).Row 'Assumes contiguous column j 
For i = 1 to LR 
    If Application.CheckSpelling(word:=Cells(i,j).Value)=False Then 
     Cells(i,j+1).Value = "SpellCheck Error!" 
    End If 
Next i 
+0

这是我需要的。谢谢! – Gorhell

0

首先,改变你的日常,能够与任何范围的作品。

Sub ColorMispelledCells(r As Range) 
    Dim c As Range 
    For Each c In r 
     if VarType(c.value2) = vbString then 
      If Not Application.CheckSpelling(c.Value2) Then 
       c.Interior.ColorIndex = 28 
      Else 
       c.Interior.ColorIndex = 0 
      End If 
     End If 
    Next 
End Sub 

变体,不着色,但在右侧单元格中写入文本 - 但请注意,这将覆盖该单元格中的任何内容。

c.Offset(0, 1) = "You have misspelled something..." 

然后,增添一分的按钮 - 这将拼写检查中使用的所有细胞(但请注意,这可能需要相当长的一段时间

sub ButtonPressed() 
    ColorMispelledCells(activesheet.usedRange) 
end sub