Help resizing table in each worksheet after macro adds new data.

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
First, the following code runs to distribute data from from an upload to several different worksheets, all of which contain tables.

VBA Code:
Sub Dispatch()
rng = shtWF.cells(1, 1).CurrentRegion

On Error Resume Next

For rw = 1 To UBound(rng)
    rRng = Right(rng(rw, 6), 4)
    Sheets(rRng).cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, UBound(rng)).Value = shtWF.cells(rw, 1).Resize(1, UBound(rng)).Value
Next rw

On Error GoTo 0
End Sub

This does a great job of adding the new data to each worksheet right below the last line of the table. However, I want to incorporate some code to resize the tables on each worksheet to include the latest data rows. Some tables have more lines than others. The code I have works on the very first worksheet but fails to loop through the rest of my worksheets. Here is that code:

VBA Code:
Sub ResizeTbls()
    Dim tbl As ListObject
    Dim lrw As Long
    Dim ws As Worksheet
    
    lrw = ActiveSheet.cells(Rows.Count, "A").End(xlUp).Row
    
    Application.ScreenUpdating = False
    For Each ws In ActiveWorkbook.Worksheets
    
        For Each tbl In ActiveSheet.ListObjects
            tbl.Resize Range("A3", "T" & lrw)
        Next
    Next
    Application.ScreenUpdating = True
End Sub

Any help is greatly appreciated!!!!! Thank you!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
if it's a table, use that table, add a listrow and write your data in that new listrow.
If you added a new listrow and you want start writing in the 2nd column (use the first for something else) then change that A1 in B1

VBA Code:
 '3 ways of assigning the same listobject (=table) (use only one)
 Set lo = Sheets(rRng).Cells(Rows.Count, 1).End(xlUp).ListObject
 Set lo = Sheets(rRng).Cells(1, 1).ListObject
 Set lo = Sheets(rRng).ListObjects("Name_Listobject")
 
 'then add a new listrow and write your data (if more then 1 row, no problem)
 lo.ListRows.Add.Range.Range("A1").Resize(1, UBound(Rng)).Value = shtWF.Cells(rw, 1).Resize(1, UBound(Rng)).Value
 
 'the same in 1 short command
 Sheets(rRng).ListObjects("Name_Listobject").ListRows.Add.Range.Range("A1").Resize(1, UBound(Rng)).Value = shtWF.Cells(rw, 1).Resize(1, UBound(Rng)).Value
 
Upvote 0

Forum statistics

Threads
1,215,700
Messages
6,126,301
Members
449,308
Latest member
VerifiedBleachersAttendee

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