Help with Hightlighting the Active Row - How to apply code to all new sheets created

kellman

New Member
Joined
Aug 25, 2012
Messages
38
Office Version
  1. 365
Platform
  1. Windows
I came across this code that works however, I could really use your help to tweak-it a little for my needs.

I would like this code to run on every new sheet that gets created within my workbook automatically, rather than me pasting this code manually into each sheet as it gets created.

There must be a more elegant solution no doubt.

From what I've read the solution has to do with adding the code to a Module.

I'm a newbie to VBA Coding and would very much appreciate your assistance in creating a solution that works for my needs.

Note - is there a way to exclude the 1st Row from the coding as the 1st Row are Column descriptions only.



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

'Clear the color of all cells
Cells.Interior.ColorIndex = 0
With Target
'Highlight row and column of the selected cell
.EntireRow.Interior.ColorIndex = 38
End With

Application.ScreenUpdating = True
End Sub
 

Attachments

  • Workbook example.png
    Workbook example.png
    44.7 KB · Views: 6
  • Sheet Code.png
    Sheet Code.png
    78.1 KB · Views: 6

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You can add similar code to the ThisWorkbook module. This code will detect the SelectionChange event in any sheet. Minimal changes are needed, see red. No code needs to be put in any sheet module.

I suggest you use code tags to paste code to preserve readability.
Rich (BB code):
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 cells
   Sh.Cells.Interior.ColorIndex = 0
   With Target
      'Highlight row and column of the selected cell
      .EntireRow.Interior.ColorIndex = 38
   End With
  
   Application.ScreenUpdating = True

End Sub

BTW note that your code will remove any fill color that was applied before the row was selected. Another way to do this is to create a conditional formatting rule that references a named range, and have the code update the named cell to the range that was just selected.
 
Upvote 0
Thank you Jeff, that works perfectly!

As for your suggestion to "create a conditional formatting rule that references a name range, and have the code update the named cell to the range that was just selected."

I have 9 columns from A to I on the first Row - could you give me a hint on how I would go about doing this?
 

Attachments

  • 1st Row Fill Color disappears using VBA Code.png
    1st Row Fill Color disappears using VBA Code.png
    17.7 KB · Views: 6
Upvote 0
The conditional formatting rule would have to applied individually to each sheet so that might not work for you.
VBA Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

   Sh.Calculate

End Sub

1732989719322.png


highlight row and column of currently selected cell.xlsm
ABCDEFGH
1TestTestTestTestTestTestTestTest
2TestTestTestTestTestTestTestTest
3TestTestTestTestTestTestTestTest
4TestTestTestTestTestTestTestTest
5TestTestTestTestTestTestTestTest
6TestTestTestTestTestTestTestTest
Row Only
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A1:H14Expression=CELL("row")=CELL("row",A1)textNO


With other formatting undisturbed:

1732989795099.png


1732989814140.png
 
Upvote 0
Solution
Also note that the code forces a recalculation on every selection change. This could cause a performance lag in a large, complex worksheet with a lot of formulas.
 
Upvote 0
Jeff - thank you for providing such great ideas to this VBA newbie.
 
Upvote 0

Forum statistics

Threads
1,225,351
Messages
6,184,453
Members
453,233
Latest member
bgmb

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top