Consulting

Results 1 to 2 of 2

Thread: VBA Adding Table Row - Quick Fix Help!

  1. #1

    VBA Adding Table Row - Quick Fix Help!

    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.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    That sounds odd, just changing a 1 to a 2 causing this. Can you post a file with this happening?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •