Resize a Listobject table dynamically with VBA

I want to resize object tables via VBA, I tried to change the code from MSDN about the method listobject.resize, but I want to dynamically if the data is loaded into 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

+13
source share
3 answers

The task Range("A1" & Lrow1)returns a range $A$112because you are passing the function the Rangeresult of the binding "A1" & "12".

Try replacing this line:

ob.Resize Range("A1" & Lrow1)

With the help of this:

ob.Resize ob.Range.Resize(Lrow1)
+15
source

There is a way to avoid calculating the last line:

Sub ResizeListDyn()
    Dim tbl As ListObject
    Set tbl = ActiveSheet.ListObjects(1)
    tbl.Resize tbl.Range.CurrentRegion
End Sub
+5
source

:

Dim tbl As ListObject

Set tbl = ActiveSheet.ListObjects("YourTableName")
With tbl.Range
    tbl.Resize .Resize(.CurrentRegion.Rows.Count) 'NOTE: unlike the Range.Resize proprty, the Table.Resize
                                                  'method argument is a Range object (not a size spec).
End With

:

With tbl.Range
    tbl.Resize .Resize(, .CurrentRegion.Columns.Count)
End With
0

Source: https://habr.com/ru/post/1659288/


All Articles