VBA copy row in tables down/specific order of adjustment

McGuilliam

New Member
Joined
Oct 23, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
I have a excel 2016 file with 13 tables on a sheet. The formulas in the 12 last tables are depended to the first table. Now I'm trying to create a macro to add a number of rows to all tables at once. I got this to work, but it is not starting with the first table. this creates a "gap" in the references, since the row in table 1 is only added later. I would like to notice it seems no solution to me to code with the table numbers because I need the code to work for other worksheets with other tables as well. I think the best way to solve this is by selecting an earlier row of a table and copying it to the last one, but I'm not sure on how to get this done. An alternative solution could of course be to influence the order of adjustment of the tables, but to me this seems unachievable. The code I got so far to insert the rows is:

VBA Code:
Sub Test()

  Dim Table As ListObject
    Dim NewRow As Range
    Dim i As Integer
    
    Answer = InputBox("How many rows do you want to add?")
Number = CInt(Answer)
    
    For i = 1 To Number
    
    Set Sheet = ActiveSheet
    
    For Each Table In Sheet.ListObjects
          Table.ListRows.Add
    Next Table
    
    Next i

End Sub

I'm rather new to VBA, so any help would be appreciated.

Thank you.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
best to declare all variables and not use Excel or VBA key words for variable names (ie: Table and Number)
try something like this
VBA Code:
    For j = 1 To ActiveSheet.ListObjects.Count
        Set tbl = ActiveSheet.ListObjects(j)
        With tbl
            For i = 1 To num
                .ListRows.Add AlwaysInsert:=True
            Next i
        End With
    Next j


 
Upvote 0
best to declare all variables and not use Excel or VBA key words for variable names (ie: Table and Number)
try something like this
VBA Code:
    For j = 1 To ActiveSheet.ListObjects.Count
        Set tbl = ActiveSheet.ListObjects(j)
        With tbl
            For i = 1 To num
                .ListRows.Add AlwaysInsert:=True
            Next i
        End With
    Next j



Thanks for your input. Unfortunately this results in the same problem, the tables get updated in the wrong order. If fixable this would still be the best solution, but I did find another workaround by merging all tables. Less clean, but it does work.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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