简体   繁体   English

Excel 2010宏以突出显示活动单元格的行

[英]Excel 2010 macro to highlight row of active cell

I'm trying to use the following macro, but it's not even recognizing it as macro so I cannot run the macro. 我正在尝试使用以下宏,但它甚至没有将其识别为宏,因此我无法运行宏。 If I change the first like to just "private/public sub test()" it will run, however then it says my Target object isn't defined. 如果我将第一个更改为“私有/公共子测试()”它将运行,但是它表示我的Target对象未定义。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target
    ' Highlight the entire row and column that contain the active cell
    .EntireRow.Interior.ColorIndex = 8
    .EntireColumn.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True
End Sub

You will have to put the macro in the code for the sheet itself, not in a separate module. 您必须将宏放在工作表本身的代码中,而不是放在单独的模块中。

What you are doing there is Event programming, which has to match with what you are trying to react to. 你在做什么是事件编程,它必须与你想要做出的反应相匹配。

it's not a macro in the sense you are used to. 在你习惯的意义上,它不是一个宏。 Events will react to something happening, and cannot be run normally. 事件将对发生的事情作出反应,并且无法正常运行。 When you select another cell (eg change selection from A1 to B2), the code you have reacts to the change of what cell is selected. 当您选择另一个单元格时(例如,将选择从A1更改为B2),您所做的代码会对所选单元格的更改做出反应。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
react when selection is changed 选择改变时做出反应
If Target.Cells.Count > 1 Then Exit Sub
If more than 1 cell is selected, then don't run the rest of the code 如果选择了多个单元格,则不要运行其余代码
Application.ScreenUpdating = False
Turn off the screen, so you can't see all the changes until we're finished 关闭屏幕,因此在完成之前您无法看到所有更改
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target

With the cell that was selected, 使用选中的单元格,
' Highlight the entire row and column that contain the active cell
.EntireRow.Interior.ColorIndex = 8
.EntireColumn.Interior.ColorIndex = 8
End With
Application.ScreenUpdating = True

and now all the coloring in has been done, switch she screen back on so the results of our work has been seen. 现在所有的着色都已完成,将屏幕切换回来,这样就可以看到我们工作的结果。
End Sub

Try this code in a module, call it as a macro: 在模块中尝试此代码,将其称为宏:

Public Sub Highlight()
  Application.ScreenUpdating = False
  ' Clear the color of all the cells
  ActiveSheet.Cells.Interior.ColorIndex = 0
  With ActiveCell
      ' Highlight the entire row and column that contain the active cell
      .EntireRow.Interior.ColorIndex = 8
      .EntireColumn.Interior.ColorIndex = 8
  End With
  Application.ScreenUpdating = True
End Sub

You have to use Public , so this sub becomes available in your macro menu of excel. 你必须使用Public ,所以这个sub在excel的宏菜单中可用。 There you can use it 'traditionally' - by wich I understand assigning a button or shortkey to it. 那里你可以'传统'使用它 - 我明白为它分配一个按钮或短按键。

This answer is based on the assumption that you want the code to run as an EVENT, after a user changes selected cell(s) 此答案基于以下假设:在用户更改所选单元格后,您希望代码作为EVENT运行

If you want this code to run in 1 worksheet only then place this in a WORKSHEET OBJECT 如果您希望此代码仅在1个工作表中运行,则将其放在WORKSHEET OBJECT中 在此输入图像描述

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub

    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Target.Parent.Cells.Interior.ColorIndex = 0
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 8
        .EntireColumn.Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True
End Sub

If you want this to run in ALL worksheets then place this in the THISWORKBOOK OBJECT (if you want to omit/include sheets you can filter on sh) 如果您希望在所有工作表中运行此项,请将其放在THISWORKBOOK OBJECT中(如果您想省略/包含可以在sh上过滤的工作表)

在此输入图像描述

Option Explicit 选项明确

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Sh.Cells.Interior.ColorIndex = 0
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 8
        .EntireColumn.Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True

End Sub

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

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