Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Listobject Table dynamically with VBA

Tags:

excel

vba

I want to change the size of the object tables through VBA, I tried modifying the code from MSDN about the listobject.resize method, but I want to dynamically if a data loaded in each row.

The code I'm trying but to no avail:

Sub resizedata()

    Dim ws As Worksheet
    Dim ob As ListObject
    Dim Lrow1 As Long

    Lrow1 = Sheets("db_goods").Cells(Rows.Count, "E").End(xlUp).Row
    Set ws = ActiveWorkbook.Worksheets("db_goods")
    Set ob = ws.ListObjects("Table1")

    ob.Resize Range("A1" & Lrow1)

End Sub

enter image description here

like image 868
Ryan Chatoeala Avatar asked Oct 29 '16 19:10

Ryan Chatoeala


1 Answers

The problem is Range("A1" & Lrow1) returns a Range of $A$112, because you are passing the Range function the result of the catenation of "A1" & "12".

Try replacing this line:

ob.Resize Range("A1" & Lrow1)

With this one:

ob.Resize ob.Range.Resize(Lrow1)
like image 172
MJH Avatar answered Oct 05 '22 09:10

MJH