Code to apply only to my table range

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I use the code below & asking for an edit please, screen shot also supplied.

Where i have my values are all in Table1 but as you can see ive added the 3 lines of text a few rows outside of the Table but the code is still being applied to it.
The table is where you see the border lines.

Please advise how i can edit the code so its only being applied to Table 1
Thanks

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim myStartCol As String
    Dim myEndCol As String
    Dim myStartRow As Long
    Dim myLastRow As Long
    Dim myRange As Range

    If Target.Cells.Count > 1 Then Exit Sub
    
    Application.ScreenUpdating = False
    
'   *** Specify columns to apply this to ***
    myStartCol = "N"
    myEndCol = "R"

'   *** Specify start row ***
    myStartRow = 4
    
'   Use first column to find the last row
    myLastRow = Cells(Rows.Count, myStartCol).End(xlUp).Row
    
'   Build range to apply this to
    Set myRange = Range(Cells(myStartRow, myStartCol), Cells(myLastRow, myEndCol))
    
'   Clear the color of all the cells in range
    myRange.Interior.ColorIndex = 6
    
'   Check to see if cell selected is outside of range
    If Intersect(Target, myRange) Is Nothing Then Exit Sub
    
'   Highlight the row and column that contain the active cell
    Range(Cells(Target.Row, myStartCol), Cells(Target.Row, myEndCol)).Interior.ColorIndex = 8
    Target.Interior.Color = vbGreen
    Application.ScreenUpdating = True

End Sub
 

Attachments

  • EaseUS_2023_08_12_09_56_36.jpg
    EaseUS_2023_08_12_09_56_36.jpg
    75.4 KB · Views: 6

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
See if this is what you had in mind.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim myRange As Range

    If Target.Cells.Count > 1 Then Exit Sub
    Set myRange = Range("Table1").ListObject.DataBodyRange
    
'   Check to see if cell selected is outside of range
    If Intersect(Target, myRange) Is Nothing Then Exit Sub
    
    Application.ScreenUpdating = False
    
'   Clear the color of all the cells in range
    myRange.Interior.ColorIndex = 6
    
'   Highlight the row and column that contain the active cell
    Intersect(Target.EntireRow, myRange).Interior.ColorIndex = 8
    Target.Interior.Color = vbGreen
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Many thanks that is what i had in mind.

Question for you,
I see lots of the code has now been removed.
Is that because Table1 specifies it all where before we had to use it ?
 
Upvote 0
Yes it does.
The table databody range runs from the first data row (the 1st row below the heading) to the last row in the table and from the first column in the table to the last column in the table.
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,068
Members
449,091
Latest member
remmuS24

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