简体   繁体   中英

Update other tables when new row inserted in lead table same worksheet

A complete novice here with coding, so some thorough guidance will be very much appreciated!

I have two tables and have created a Command button which adds a new row to the lead table (Table1).

What I need to do is for the second, third and fourth tables (Table2,3,4) to be updated with a new row and with the same information in Column C when a new row is inserted in the lead table.

So far this is the code I have for the Command button which adds new rows to the Table1, ie lead table.

Range("B15:P15").End(xlDown).Select
ActiveCell.EntireRow.Insert

See table here

Might I suggest that rather than using sorting to group your tables, you use ListObjects ? If you do this, there is a default collection of them for each sheet and you can easily implement adding rows to each table.

Code to do so would look something along the lines of

Sub add_rows()

    Dim tws     As Worksheet, _
        lo      As ListObject, _
        lr      As ListRow, _
        iter    As Long
        
    
    Set tws = Application.ThisWorkbook.Sheets()
    
    '' treat all tables the same
    For Each lo In tws.ListObjects
        Set lr = lo.ListRows.Add
        '' example of how to set listrow data
        Let lr.Range(1, 1).Resize(1, 5) = [{1,2,3,4,5}]
    Next lo
       
    '' or
    
    '' treat each table differently
    For iter = 1 To tws.ListObjects.Count
        Set lo = tws.ListObjects.Item(iter)
        Set lr = lo.ListRows.Add
        
        Select Case iter
            Case 1
                '' do a first thing
            Case 2
                '' do a second thing
            Case 3
                '' do a third thing
            Case Else
                '' do something gneric
        End Select
    Next iter    
End Sub

where the only major change you would need to do to your excel file would be to select each table, and press ctrl + T to convert it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM