PDA

View Full Version : VBA Adding Table Row - Quick Fix Help!



patricevease
01-11-2016, 07:48 AM
Hi all,


I am having a problem with my VBA code. On my sheet I have 3 tables (Table 7,15,16). I am trying to create a button that will allow me to select which table and then add a row to it. So far I have only been able to create a code that applies to one table at a time. However when I try and change it to create a new button for the next table the code breaks, as explained below.



Sub TableTest()
Dim the_sheet As Worksheet
Dim table_list_object As ListObject
Dim table_object_row As ListRow


Set the_sheet = ActiveSheet
Set table_list_object = the_sheet.ListObjects(2)
Set table_object_row = table_list_object.ListRows.Add


table_object_row.Range(1, 1).Value = ""


When I change 'the_sheet.ListObjects(1)' to 'the_sheet.ListObjects(2)' the code picks up Table 15 (as I want it to) however will not add a row to it.
The problem is that 'set table_object_row = table_list_object.ListRows.Add' when highlited says 'set table_object_row = nothing'.


Can anyone see where I need to correct my code? All I am doing is changing the ListObjects(1) to ListObjects (2) and the next line of code just breaks.


> Alternatively, if anyone knows how to create a single code that when run will allow me to choose which table I want to add a row to that would be extremely helpful!


Thanks in advance.

p45cal
01-11-2016, 10:23 AM
That sounds odd, just changing a 1 to a 2 causing this. Can you post a file with this happening?