[英]Excel macro to highlight all cells that match value in current cell
I'm looking for a macro that will automatically highlight any cells in the current worksheet if the value of those cells is the same as the currently-selected cell. 我正在寻找一个宏,该宏将自动突出显示当前工作表中的任何单元格(如果这些单元格的值与当前选定的单元格相同)。 So if cell B3 is currently selected, and it contains the value 3, then all other cells with a value of 3 will be highlighted.
因此,如果当前选择了单元格B3,并且该单元格包含值3,则所有其他值为3的单元格都将突出显示。
Any ideas? 有任何想法吗?
@Reafidy provided a good macro and this will do the same with conditional formatting @Reafidy提供了一个很好的宏,这将与条件格式相同
Sub HighLightCells()
ActiveSheet.UsedRange.Cells.FormatConditions.Delete
ActiveSheet.UsedRange.Cells.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=ActiveCell
ActiveSheet.UsedRange.Cells.FormatConditions(1).Interior.ColorIndex = 4
End Sub
Put this in the sheet selection change event 将此放入工作表选择更改事件
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
call HighLightCells
End Sub
Use conditional formatting. 使用条件格式。
If you really need a macro then: 如果您确实需要宏,则:
Sub HighlightCells()
Dim rCell As Range
If ActiveCell.Value = vbNullString Then Exit Sub
Set rCell = ActiveCell
Do
Set rCell = ActiveSheet.UsedRange.Cells.Find(ActiveCell.Value, rCell)
If rCell.Address <> ActiveCell.Address Then
rCell.Interior.Color = 65535
Else
Exit Do
End If
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.