Capturing the auto-insertion of a row to a table via a Tab in VBA

gravanoc

Active Member
Joined
Oct 20, 2015
Messages
346
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have written code to capture adding a row using the command or when typing new data in a blank cell in a row beneath the last table row, however, when someone tabs at the end of the last row of data and in the last cell, it will automatically insert a new row in the table. This doesn't trigger any normal events that I have setup. Anyone know how to tell when this has happened?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Probably you can use the WorksheetChange event: when a new row is added to the table Target.Address will refer to the last row of the table; also, when you tab at the end on the table, the selection will move to cell 1 of the table row.
So probably this code will catch the situation:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Ck1 As Boolean, Ck2 As Boolean
'
On Error Resume Next
Ck1 = (Selection.ListObject.ListRows(Selection.ListObject.DataBodyRange.Rows.Count).Range.Cells(1, 1).Address = Selection.Address)
Ck2 = (Selection.ListObject.ListRows(Selection.ListObject.DataBodyRange.Rows.Count).Range.Address = Target.Address)
On Error GoTo 0
If Ck1 And Ck2 Then
    'what to do When new line is addedd
    'what to do When new line is addedd
    MsgBox ("NewLine manually Inserted")
End If
End Sub
The code has to be inserted into the vba code sheet of the working sheet:
-rightclick on the tab with the name of the sheet and chose View Code to open the vba editor at the right page
-copy the code and paste it into the right frame of the page

Return to Excel and test for the various conditions

Bye
 
Upvote 0
Solution
Probably you can use the WorksheetChange event: when a new row is added to the table Target.Address will refer to the last row of the table; also, when you tab at the end on the table, the selection will move to cell 1 of the table row.
So probably this code will catch the situation:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Ck1 As Boolean, Ck2 As Boolean
'
On Error Resume Next
Ck1 = (Selection.ListObject.ListRows(Selection.ListObject.DataBodyRange.Rows.Count).Range.Cells(1, 1).Address = Selection.Address)
Ck2 = (Selection.ListObject.ListRows(Selection.ListObject.DataBodyRange.Rows.Count).Range.Address = Target.Address)
On Error GoTo 0
If Ck1 And Ck2 Then
    'what to do When new line is addedd
    'what to do When new line is addedd
    MsgBox ("NewLine manually Inserted")
End If
End Sub
The code has to be inserted into the vba code sheet of the working sheet:
-rightclick on the tab with the name of the sheet and chose View Code to open the vba editor at the right page
-copy the code and paste it into the right frame of the page

Return to Excel and test for the various conditions

Bye
I was being stupid and didn't realize I had events off when I was trying earlier, and I soon realized that it was captured normally by Selection change. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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