简体   繁体   中英

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.

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.

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. 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)

If you want this code to run in 1 worksheet only then place this in a 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)

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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