繁体   English   中英

突出显示所选单元格的周围单元格

[英]Highlight surrounding cells of selected cell

我正在尝试在 Excel 中练习 Levenshtein 距离。要填充单元格,我们需要考虑三个单元格(左、左上和上)中的最小值。 如果它们被突出显示,很容易找到这三个中的最小值。

每当我将 cursor 放在任何空单元格上时,我都想突出显示这三个单元格。 就像下图所示。 当我把我的 cursor 放在 C3 上时; B2、B3 和 C2 应突出显示。

我找到了一个 VBA 脚本。 但它突出显示了 cursor 单元格的整个行和列。 我不熟悉 VBA,因此无法按照我的方式修改行和列。

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
'Update 20200430
Static xRow
Static xColumn
If xColumn <> "" Then
    With Columns(xColumn).Interior
        .ColorIndex = xlNone
    End With
    With Rows(xRow).Interior
        .ColorIndex = xlNone
    End With
End If
pRow = Selection.Row
pColumn = Selection.Column
xRow = pRow
xColumn = pColumn
With Columns(pColumn).Interior
    .ColorIndex = 6
    .Pattern = xlSolid
End With
With Rows(pRow).Interior
    .ColorIndex = 6
    .Pattern = xlSolid
End With
End Sub

这就是它的作用

找到的代码的输出

工作表选择更改:突出显示单元格

Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target.Cells(1)
        If .Row = 1 Then Exit Sub
        If .Column = 1 Then Exit Sub
        If IsEmpty(.Cells) Then
            .Worksheet.UsedRange.Interior.ColorIndex = xlNone
            Union(.Offset(-1, -1).Resize(2), .Offset(-1)) _
                .Interior.Color = vbYellow
        End If
    End With
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM