Intersect Method using a variable range

benzids

New Member
Joined
Apr 15, 2024
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am attempting to create a macro that will run every time a table is updated by adding an additional row to the table. I am basing my solution on the Microsoft article here Run a macro when certain cells change in Excel - Microsoft 365 Apps

In order for their example to work, I need the value of KeyCells to change to the row beneath the table. This is what I have so far, I understand it is certainly not elegant and I welcome any improvements to the syntax as well. My code also doesn't finish the job of actually copying and pasting the data, but figuring out how to actually select it has stumped me before I got that far.

VBA Code:
Dim LR As Long
LR = Range("A:A").SpecialCells(xlCellTypeLastCell).Row

Dim part1 As String
Dim part2 As String
part1 = "A" & LR
part2 = "F" & LR


Dim copy_area As String
copy_area = (part1 & ":" & part2)
Range(copy_area).Select

Dim KeyCells As Range
Set KeyCells = Range(copy_area)

If Not Application.Intersect(KeyCells, Range(Target.Address))
    Is Nothing Then
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Maybe something like this?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim FirstCol As String, LastCol As String, copy_area As String
    Dim LR As Long

    FirstCol = "A"
    LastCol = "F"
    LR = Me.Range(FirstCol & Me.Rows.Count).End(xlUp).Row

    copy_area = (FirstCol & LR & ":" & LastCol & LR)
    Set KeyCells = Range(copy_area).Offset(1)         ' this selects the first row after the table
    KeyCells.Select

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        '
        'Do stuff here
        '
    End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

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