如果find一个单词,请更改列中的所有单元格

我有一个6列的工作表,我想匹配列E中的单词,如果它在该列中的任何单元格,然后用该单词replace整个列。

Sub Macro8() Columns("E:E").Select If Columns("E:E").Select = "dog" Then Entire.Columns("E:E").Select = "dog" End Sub 

就像我在上面的评论中提到的,使用Worksheet_Change事件。 但是如果你想在你方便的时候运行一个例程,那就使用这个代码

 Sub Sample() Dim ws As Worksheet Dim lRow As Long '~~> Change this to the relevant sheet Set ws = ThisWorkbook.Sheets("Sheet1") With ws '~~> Find Lastrow in Col E which has data lRow = .Range("E" & .Rows.Count).End(xlUp).Row '~~> Use Countif to check for an occurance of Dog in Col E If Application.WorksheetFunction.CountIf(.Columns(5), "Dog") _ Then .Range("E1:E" & lRow).Value = "Dog" End With End Sub