Auto resize (Shrink) table

rlekkala

New Member
Joined
Feb 3, 2009
Messages
49
Say I have 2X3 table. If I added any values in the next column or row the table automatically expands as excel 2007 currently does. What I want is how to shrink if the row or column is removed?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,896
Responding to an old post, but I had this code to remove blank rows from a table.
Code:
Sub SortAndShrinkTableRows(sWorksheet As String, sTableName As String)
    
    Dim arytableParms As Variant
    Dim lFirstRow As Long
    Dim lDataRowCount As Long
    'Sort table by its 1st column to get empty cells to bottom rows
    'Resize table to contain only rows with data in first column
    
    With Worksheets(sWorksheet).ListObjects(sTableName)
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range(.Range.Columns(1).Address), _
            SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortTextAsNumbers
        With .Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
        'Show no blanks
        .Range.AutoFilter Field:=1, Criteria1:="<>"
        lDataRowCount = Application.WorksheetFunction.Subtotal(3, Range(sTableName).Columns(1))
        .Range.AutoFilter
        
        arytableParms = Split(.Range.Address, ":")
        lFirstRow = Split(arytableParms(0), "$")(2)
        .Resize Range(arytableParms(0) & ":$" & _
            Split(arytableParms(1), "$")(1) & "$" & _
            lFirstRow + lDataRowCount + IIf(lDataRowCount = 0, 1, 0))
    End With

End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,486
Messages
5,831,968
Members
430,099
Latest member
rdhoy

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
Top