Macro to Delete x Rows from All Tables on Sheet

captainxcel

New Member
Joined
Jul 28, 2017
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Hello and Thanks in advance. I use the following code to add one or more rows to all tables in a worksheet (the background is that I have a worksheet with over 100 tables that needs a 1 for 1 row count correspondence with another table). How would I modify the code in order to delete one or more rows from all tables in the worksheet?
Thanks

VBA Code:
Sub addSomeRowsToAllTablesOnSheet()

Dim tbl As ListObject
Dim NewRow As ListRow
Dim i As Integer
Dim new_rows As Integer

new_rows = InputBox("How many row(s) would you like to add to each table:", 1)

For i = 1 To new_rows
For Each tbl In Worksheets("Test").ListObjects
    Set NewRow = tbl.ListRows.Add
Next tbl
Next i

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You did not say where you want the rows removed from (first row, last row, etc). How about this to remove rows from the last row and up...

VBA Code:
Sub deleteSomeRowsToAllTablesOnSheet()

    Dim tbl As ListObject
    Dim NewRow As Long
    Dim i As Integer
    Dim new_rows As Integer
    
    new_rows = InputBox("How many row(s) would you like to delete from each table:", 1)
    
    For i = 1 To new_rows
    For Each tbl In Worksheets("Test").ListObjects
        NewRow = tbl.ListRows.Count
        tbl.ListRows(tbl.ListRows.Count).Delete
    Next tbl
    Next i

End Sub
 
Upvote 0
Solution
Good point!. Your assumption was correct - I do want the rows to come off of the bottom of the table. I'll try this out and mark your reply. Thanks!
 
Upvote 0
That works great. Thanks again! Needed that count to do it. Makes sense.
 
Upvote 0
You're welcome. I was happy to help. Thanks for the feedback.
 
Upvote 0
I was working with what you had, but this line can be deleted...

VBA Code:
NewRow = tbl.ListRows.Count
 
Upvote 0
How about one code that does both, delete or add...

VBA Code:
Sub addORdeleteSomeRowsToAllTablesOnSheet()

    Dim tbl As Object
    Dim new_rows As Integer
    Dim rng As Range
    
    new_rows = InputBox("How many row(s) would you like to add/delete from each table:" _
        & vbNewLine & vbNewLine & "Postive number to add -> 3 or +3" & vbNewLine & _
        "Negative number to delete -> -2" & vbNewLine, "ADD or DELETE LINES", 1) + 1
    
    For Each tbl In Worksheets("Test").ListObjects
        Set rng = Range(tbl & "[#All]").Resize(tbl.ListRows.Count + new_rows, tbl.ListColumns.Count)
        tbl.Resize rng
    Next tbl
  
End Sub
 
Upvote 0
Wow. This is great. Thank you. Was out of the office for a couple of days so sorry for the late reply. Appreciate your help on this.
 
Upvote 0
I was happy to help. I thought of resize while doing something else so I thought I could apply it here...

Thanks for the update.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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