Macro for Resizing a table after COUNTA

Prinny

New Member
Joined
Oct 24, 2015
Messages
14
Hi all,

This is probably an easy solution but this is my first attempt at writing a macro.

I have a simple spreadsheet with a table in it (Table1). the table runs from Column A to Column K

I am trying to write a macro that will count all the rows in my worksheet that have data in and then re-size the table to have enough rows to incorporate all the data. This is to ensure that all the data is added to my table, even when other users have just pasted it into the worksheet.

I assume I have to count all the rows with data in by using COUNTA and then resize the table. Unfotunately, I keep getting errors on the penultimate line. I don't know if this is a syntax error or what is going wrong but I don't know how to fix it. Can anyone help?

My code is:

Code:
Sub AddRowsToTable()

'Find how many rows of data exist
Dim DRWS As Integer
DRWS = WorksheetFunction.CountA(ActiveSheet.Rows)

' select the table and resize it
ActiveSheet.ListObjects("Table1").Range.Select
Dim tblrng As Range
Dim tbl As ListObject
 
Set tbl = ActiveSheet.ListObjects("Table1")
  
Set tblrng = Range("Table1[#All]").Resize("A1:K" & DRWS)   'This line is bringing up errors

tbl.Resize tblrng

End Sub
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,214,905
Messages
6,122,175
Members
449,071
Latest member
cdnMech

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