首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >突出显示选定行的VBA将导致整个行的选择

突出显示选定行的VBA将导致整个行的选择
EN

Stack Overflow用户
提问于 2015-12-21 19:33:23
回答 1查看 1.3K关注 0票数 0

我编写了一段代码,突出显示选定的行。如果选择更改-新选定的行将被高亮显示,并且前面的选择的格式返回到初始的。我使用

  • 第9行作为突出显示格式示例和
  • 第10行作为未选定行的条件格式的基线。

密码很好用。但是,当单元格被选中时,行将被高亮显示,所选单元格保持活动状态,但整个行将被选中。有人能帮我取消除目标单元以外的所有东西吗?

来自here的任何东西都没有帮助。

代码语言:javascript
运行
复制
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

LastRowA = Range("A" & Rows.Count).End(xlUp).Row

If Target.Cells.Count > 1 Or Target.Cells.Count < 1 Then 'If selected 1 cell
    'Do nothing
Else
    Application.ScreenUpdating = False
    If Target.Row > 10 And Target.Row < LastRowA + 1 Then

        Rows("10:10").Copy 'Restore all rows to custom conditional formatting of row 10
        For tableRow = 11 To LastRowA 
            Rows(tableRow & ":" & tableRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Next tableRow

        Rows("9:9").Copy 'Highlight active row using formating of row #9
        Rows(Target.Row).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        Application.CutCopyMode = False
        Target.Cells.Activate 'Return Target to initially selected cell
    End If
    Application.ScreenUpdating = True
End If

End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-21 19:58:45

尝尝这个

代码语言:javascript
运行
复制
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRowA As Long
Dim tableRow As Long

LastRowA = Range("A" & Rows.Count).End(xlUp).Row

If Target.Cells.Count > 1 Or Target.Cells.Count < 1 Then 'If selected 1 cell
    'Do nothing
Else
    Application.ScreenUpdating = False
    If Target.Row > 10 And Target.Row < LastRowA + 1 Then

        Rows("10:10").Copy 'Restore all rows to custom conditional formatting of row 10
        For tableRow = 11 To LastRowA
            Rows(tableRow & ":" & tableRow).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Next tableRow

        Rows("9:9").Copy 'Highlight active row using formating of row #9
        Rows(Target.Row).PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

        Application.CutCopyMode = False
        Target.Cells.Activate 'Return Target to initially selected cell
        Application.EnableEvents = False
        Target.Cells.Select
        Application.EnableEvents = True
    End If
    Application.ScreenUpdating = True
End If

End Sub
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34403033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档