Add Code when new data is added

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
559
I'm using the following code on my worksheet page and want to know how I can expand this so that if I insert data in row 2 it will add another line as shown.

If Target.Address = "$A$1" Then
Call Ghostbusters
End If

Add this:

If Target.Address = "$B$1" Then
Call Ghostbusters
End If
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016
Hi, try something like this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
    Call Ghostbusters
End If
End Sub
 

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
559
Sorry i meant that the data would be added to row 2. I updated the code to reflect this, but i think it is now preventing me from using the code that adds a row. Is there a way to say Range (A1:LR)
and the define LR. So that it would look and A1 and then count every row under that, that has a value in A
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lstRow As Long
lstRow = Range("A" & Rows.Count).End(xlUp).Row
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:A" & lstRow)) Is Nothing Then
    Call Ghostbusters
End If
End Sub

OR

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A:A")) Is Nothing Then
    Call Ghostbusters
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,910
Messages
5,627,581
Members
416,255
Latest member
amethystia

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
Top