[示例2]在B列中标出A列中有相应值的单元格 Sub Mark_cells_in_column() Dim FirstAddress As String Dim myArr As Variant Dim rng As Range Dim I As Long Application.ScreenUpdating = False myArr = Array("VBA") '也能够在数组中使用更多的值,如下所示 'myArr = Array("VBA", "VSTO") With Sheets("Sheet2").Range("A:A") .Offset(0, 1).ClearContents '清除右侧单元格中的内容 For I = LBound(myArr) To UBound(myArr) Set rng = .Find(What:=myArr(I), _ After:=.Cells(.Cells.Count), _ LookIn:=xlFormulas, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) '如要想查找rng.value中的一部分,可使用参数值xlPart '如果使用LookIn:=xlValues,也会处理公式单元格中与条件相同的值 If Not rng Is Nothing Then FirstAddress = rng.Address Do rng.Offset(0, 1).Value = "X" '如果值VBA找到,则在该单元格的右侧列中的相应单元格作上标记 Set rng = .FindNext(rng) Loop While Not rng Is Nothing And rng.Address <> FirstAddress End If Next I End With Application.ScreenUpdating = True End Sub 示例说明:运行程序后,将查找工作表Sheet2上A列中的每个单元格,并在值为“VBA”所在的单元格的右侧单元格中作出标记“X”。 |