Please help me explain Target(index) in Worksheet_Change Event?

hunghung

New Member
Joined
Feb 27, 2018
Messages
19
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi, I am new to VBA and this is the first time I post a thread in the forum. Hope you in the forum help me.
I have trouble with one problem. Sorry for my bad English

I have data containing 3 columns Code, Name and Date with alternate blank rows as below. I want a code that can automatically insert more a blank row down when I enter data into yellow highlighted blank rows.
BCDE
CodeNameDate
TD20THEP TRON Ø20
TD22THEP TRON Ø22
TD24THEP TRON Ø24
TD26THEP TRON Ø28

<tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #00B0F0, align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]

[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]12/4/2014[/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]

[TD="align: right"]11/1/2014[/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]

[TD="align: right"]11/1/2014[/TD]
[TD="align: right"][/TD]

[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]10[/TD]

[TD="align: right"]11/1/2014[/TD]
[TD="align: right"][/TD]

[TD="align: center"]11[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet1
A online friend helped me create this code but no explain his code for me.
Code:
Option Explicit


Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, [D2:D1000]) Is Nothing Then
   Application.EnableEvents = False
      If Target(2) <> "" Then Target(2).EntireRow.Insert
   Application.EnableEvents = True
End If
End Sub

First thing, I want people in the forum please explain what is the meaning of Target(2) and the usage of it.
Secondly, if possible, is there any other solution for this problem? I wanna see your other solutions.
Thanks in advance.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi hunghung,
The target cell is the cell that is changed
target(2) is the cell below
target(,2) is the column to right
and target(2,2) is the diagonal cell
target(1) is the same as target(1,1) or target (,1)

so your code is looking form a blank cell below the entry data and if is not balnk adds a new row.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

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