VBA - Select only table

sparkytech

Board Regular
Joined
Mar 6, 2018
Messages
96
Office Version
  1. 365
  2. 2019
I have the code below (created by others) that highlights changes to cells. It currently does this for the entire sheet. How can I limit this to the table?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Then Exit Sub
Dim i As Double
i = ActiveSheet.Cells(Application.Rows.Count, "A").End(xlUp).Row
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
If Target.Row > i Then
Range(Cells(Target.Row, 1), Cells(Target.Row, 30)).Interior.ColorIndex = 46
ElseIf Target.Row <= i Then
Target.Interior.ColorIndex = 19
End If
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Maybe something like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.ListObjects(1).DataBodyRange) Is Nothing Then
        Target.Interior.ColorIndex = 19
    End If
End Sub
 
Upvote 0
Maybe something like:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.ListObjects(1).DataBodyRange) Is Nothing Then
        Target.Interior.ColorIndex = 19
    End If
End Sub
Thanks for the code! Stupid question, I know... how do I integrate this into my existing code?
 
Upvote 0
From reading through your original code it looked like it was making the amended cells turn a different colour.

The code provided will colour anything inside the table that has been amnded. If that is all it needs to do then my code would replace yours.

Are there any other requirements of the code other than to colour changed cells inside the table?
 
Upvote 0
I assumed the other parts to the code were being applied to anything outside of the table.
 
Upvote 0
I assumed the other parts to the code were being applied to anything outside of the table.
As I mentioned, I didn't create the code, and haven't reverse engineered it yet (still learning VBA). :)

I need the code to only apply to the table, and actually limit it to columns B:W. The only function I need is to highlight cells that are changed. With that being said, I couldn't get your code to work. I put it in the sheet, replacing my original code. Is that the correct place to put this? Thanks again!
 
Upvote 0
What is the name of your table?
What columns are your table in?
Where exactly is the "other" data you want to exclude located?
 
Upvote 0
What is the name of your table?
What columns are your table in?
Where exactly is the "other" data you want to exclude located?
Name of table is Master_Table. Table is in columns A:AB (these cells need the "change" highlight, need to exclude columns A, and X:AB.
 
Upvote 0
Name of table is Master_Table. Table is in columns A:AB (these cells need the "change" highlight, need to exclude columns A, and X:AB.
Correction... table is A:AB, only columns B:W need highlights. Columns A and X:AB do not need highlights.
 
Upvote 0
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Target, Range("Master_Table"))
    
'   Exit if no updates to table
    If rng Is Nothing Then Exit Sub
    
'   Loop through updated cells
    For Each cell In rng
'       See if update in columns B:W
        If cell.Column >= 2 And cell.Column <= 23 Then
            cell.Interior.ColorIndex = 19
        End If
    Next cell

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

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