Event Enabled Macro - Change after inserting row

zack_

Board Regular
Joined
Apr 18, 2014
Messages
79
Good Morning Everyone!

This is the first time I have ever attempted this sort of macro. I am more or less testing the logic to see if it works. The macro appears to be working, but I was wondering if anyone saw potential pitfalls in the code.

Eventually, I plan to add it to a worksheet so that when rows are inserted within a specified range it will be take the formulas from the above row and copy them down to the newly inserted row.

Here is the code, thanks in advance for taking a look!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


Dim iFinalRow As Integer
iFinalRow = Cells(Rows.Count, 1).End(xlUp).Row


' Is target in desired range AND has the range expanded due to rows being inserted


If Not Application.Intersect(Target, Me.Range(Cells(5, 1), Cells(iFinalRow, 1))) Is Nothing And _
   Target.CurrentRegion.End(xlDown).Row <> iFinalRow Then
     
  ' At least one cell of Target is within the range. Change above row color
     
     Range("a1").Value = "Blue"
           
Else
        ' No cell of Target is in the range A5: Last row with data. Get Out.
    
     Exit Sub
End If


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I would change Integer to Long in case your data increases beyond the scope of an Integer variable.
 
Upvote 0
the below looks incorrect as you will already have inserted the row meaning the iFinalRow will equal your target region
 
Upvote 0
the below looks incorrect as you will already have inserted the row meaning the iFinalRow will equal your target region


Target.CurrentRegion.End(xlDown).Row <> iFinalRow Then
 
Upvote 0
the below looks incorrect as you will already have inserted the row meaning the iFinalRow will equal your target region

hey barry,

could you explain a little more? I think I may be confused on the order of how the program is read. in my head, the variable is created and then as the row is inserted the current region allows the entire range to be selected and the last row found, which is one greater than ifinalrow....

but I guess what you are saying, is the variable is not created until after the event, inserting the row, so the two are the same?
 
Upvote 0
but I guess what you are saying, is the variable is not created until after the event, inserting the row, so the two are the same?

Exactly, so you'll insert the row then the worksheet event will run assigning the variable value.

you can assign the last row value upon activation of the sheet like below

Code:
Option Explicit
Public lr As Long
Private Sub Worksheet_Activate()
Let lr = Range("A" & Rows.Count).End(xlUp).Row
End Sub
Sub test()
MsgBox lr
End Sub
 
Upvote 0
then you can use lr in your other change worksheet event without having to assign a value to it.
 
Upvote 0
Code:
Option Explicit
Public lr As Long
Private Sub Worksheet_Activate()
Let lr = Range("A" & Rows.Count).End(xlUp).Row
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)

' Is target in desired range AND has the range expanded due to rows being inserted


If Not Application.Intersect(Target, Me.Range(Cells(5, 1), Cells(iFinalRow, 1))) Is Nothing And _
   Target.CurrentRegion.End(xlDown).Row <> lr Then
     
  ' At least one cell of Target is within the range. Change above row color
     
     Range("a1").Value = "Blue"
           
Else
        ' No cell of Target is in the range A5: Last row with data. Get Out.
    
     Exit Sub
End If


End Sub
 
Upvote 0
Exactly, so you'll insert the row then the worksheet event will run assigning the variable value.

you can assign the last row value upon activation of the sheet like below

Code:
Option Explicit
Public lr As Long
Private Sub Worksheet_Activate()
Let lr = Range("A" & Rows.Count).End(xlUp).Row
End Sub
Sub test()
MsgBox lr
End Sub

Awesome. So from there, I would just call that variable from the worksheet change event macro, and compare it to the target range to see if it was expanded? ... thanks a lot! i think this makes sense. i'm going to tinker around and test. i appreciate all the quick feedback Barry
 
Upvote 0

Forum statistics

Threads
1,203,752
Messages
6,057,150
Members
444,908
Latest member
Jayrey

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