Highlight cross section/interest of current cell in table

gazmoz17

Board Regular
Joined
Sep 18, 2020
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Hi,

Wish to colour fill my current cell within active table.

Manged to do it via this code. But how do I automatically get this to work in any workbook/any active table? Also the current code removes existing colour fill both within the table and outside of the table. Is there a way to preserve exisitng colour fill once current cell is de-selected.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    
    Dim tbl As ListObject
    Dim tblRange As Range
    Dim tblHeader As Range

    ' Check if the selected cell is within a table
    Set tbl = Nothing
    Set tbl = ActiveSheet.ListObjects(Target.ListObject.Name)
    
    ' If a table is found, get the table range and header range
    If Not tbl Is Nothing Then
        Set tblRange = tbl.DataBodyRange
        Set tblHeader = tbl.HeaderRowRange
        
        ' Clear formatting in the entire worksheet
        Cells.Interior.ColorIndex = xlColorIndexNone
        
        ' Check if the selected cell is within the table range
        If Not Intersect(Target, tblRange) Is Nothing Then
            ' Get the entire column of the active cell
            Dim targetColumnIndex As Long
            targetColumnIndex = Target.Column - tblRange.Columns(1).Column + 1
            
            ' Check if the target column index is within the valid range of the table
            If targetColumnIndex >= 1 And targetColumnIndex <= tbl.ListColumns.Count Then
                ' Apply formatting to the active column within the table
                On Error Resume Next
                tbl.ListColumns(targetColumnIndex).DataBodyRange.Interior.ColorIndex = 37
                tbl.ListRows(Target.Row - tblRange.Rows(1).Row + 1).Range.Resize(, tbl.ListColumns.Count).Interior.ColorIndex = 37
                tblHeader.Columns(targetColumnIndex).Interior.Color = RGB(255, 165, 0)
                On Error GoTo 0
                
                ' Clear formatting for the active cell
                Target.Interior.ColorIndex = xlColorIndexNone
            End If
        End If
    End If
    
    ' Clear formatting if the selected cell is outside the table range
    If tbl Is Nothing Or Intersect(Target, tblRange) Is Nothing Then
        Cells.Interior.ColorIndex = xlColorIndexNone
    End If
    
    On Error GoTo 0
End Sub

Many Thanks
gareth
 

Attachments

  • Highlight table active cell.PNG
    Highlight table active cell.PNG
    18.1 KB · Views: 10

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Use conditional formatting

Create a new rule.

On the Home tab, in the Styles group, click Conditional formatting > New Rule… In the New Formatting Rule window, select Use a formula to determine which cells to format. Enter the formula in the corresponding box.

Excel Formula:
=OR(ROW()=CELL("row"),COLUMN()=CELL("col"))

Click the Format… button to choose your custom format.

Put the following code in the events of your sheet.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = True
End Sub


Review:

😊
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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