突出显示选定细胞的细胞周围的细胞

发布于 2025-01-27 00:53:17 字数 1018 浏览 3 评论 0原文

我正在尝试在Excel中行使Levenshtein距离。要填充细胞,我们需要考虑至少三个单元格(左,向上和向上)。如果强调这三个,则很容易找到至少这三个。

每当我将光标放在任何空单元上时,我都想突出显示这三个单元格。就像下图所示。当我将光标放在C3上时; B2,B3和C2应受到刺眼。

我找到了一个VBA脚本。但是它刺激了光标单元格的整个行和列。我不熟悉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

这就是它做的

“找到的代码的输出”

I am trying to exercise Levenshtein Distance in Excel. To fill the cells, we need to consider the minimum of three cells (left, up-left, and up). It is easy to find minimum of those three if they were highlighted.

I want to highlight those three cells whenever I put my cursor on any empty cell. Just like shown on image below. When I put my cursor on C3; B2, B3, and C2 should be higlighted.

I found a VBA script. But it higlightes the entire row and column of cursor cell. I am not familiar with VBA, therefore can't modify rows and columns to my way.

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

this is what it does

Output of code that Found

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2025-02-03 00:53:17

一个工作表SelectionChange:突出显示单元格

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

A Worksheet SelectionChange: Highlight Cells

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文