Resize table

2022

Board Regular
Joined
Jun 5, 2022
Messages
74
Office Version
  1. 2016
Platform
  1. Windows
I have a table in a tab called Sheet2 that looks as follows:

The text 'ID' in cell G2, the word 'Food' in H2.

Then the numbers 1 and 2 in G3 and G4, respectively.

And the words 'Chocolate' and 'Tomato' in H3 and H4, respectively.

When I created the table the range of cells in it, if you go to the 'Name Manager' are G3:H4 in Sheet2

And I would like to resize the table using VBA by extending the bottom of it to cover the number of rows added to in column G.

So if someone added a new value in cell G5 (eg the number 3, which would then mean you have 1. 2 and 3 in cells G2:G5), then code would increase the table size so that it would now cover cells G3:H5 instead of G3:H4.

Does anyone know how I can modify the code so that it expands the number of rows the table covers, based in new row entries in column G (using cell references eg "G1", instead of the exisiting cell notation)?

It currently gives an error when it gets to the line that starts "Set Table = ThisWorkbooks.Sheets("Sheet2").ListObjects("Table1")" (even though the table is called Table1).

VBA Code:
Sub resizetable()


Sheet2.Activate


Dim LastRow As Long

Dim Table As ListObject


LastRow = Cells(Rows.Count, "G").End(xlUp).Row


Set Table = ThisWorkbooks.Sheets("Sheet2").ListObjects("Table1")


Table.resize Range(Cells(3, 7), Cells(LastRow, 8))


End Sub




IDFood
1​
Chocolate
2​
Tomato
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Rich (BB code):
ThisWorkbooks.Sheets

Lose the "s"
VBA Code:
ThisWorkbook.Sheets
 
Upvote 0
When I created the table the range of cells in it, if you go to the 'Name Manager' are G3:H4 in Sheet2

And I would like to resize the table using VBA by extending the bottom of it to cover the number of rows added to in column G.

So if someone added a new value in cell G5 (eg the number 3, which would then mean you have 1. 2 and 3 in cells G2:G5), then code would increase the table size so that it would now cover cells G3:H5 instead of G3:H4.
In what circumstances does the table not automatically expand when values are added in column G?
 
Upvote 0
I'm just wondering whether it's a true table, or simply a range 'named' Table1. When you select a cell within the table, say G3, do you get the menu Table Design appearing as a new menu option?
 
Upvote 0
In what circumstances does the table not automatically expand when values are added in column G?
The first circumstance is if you add a column of data to precede the first column of the table. So the table is G3:H4.

But what if I want to add data in cells F2:F4 (a header in F2 and numbers in F3 and F4, respectively) - how can I resize the table using VBA, to prevent having to resize it manually?

And the second circumstance is decreasing the table size.

So if the table goes from G3:H4 (with G2:H2 as headers) but the latest set of data that goes into the table only goes up to row 3 instead of row 4, how can I resize the table so that it shrinks the table so that the table only has the headers and one row of input data or it covers G3:H3 instead of G3:H4?

I tried this, but the line before 'End Sub' failed.....do you know why, please?

VBA Code:
Sub resizetable()


Sheet2.Activate


Dim LastRow As Long

Dim Table As ListObject


LastRow = Cells(Rows.Count, "G").End(xlUp).Row


Set Table = ThisWorkbook.Sheets("Sheet2").ListObjects("Table1")

'Cells(3,7) means row 3 and column G (G is the 7th column)

Table.resize Range(Cells(3, 7), Cells(LastRow, 8))

End Sub
 
Upvote 0
I'm just wondering whether it's a true table, or simply a range 'named' Table1. When you select a cell within the table, say G3, do you get the menu Table Design appearing as a new menu option?
Yes.

I think that the addition of the data described in my latest reply above is what may have caused some confusion.....

Please let me know if you want me to clarify further?
 
Upvote 0
The first circumstance is if you add a column of data to precede the first column of the table. So the table is G3:H4.

And the second circumstance is decreasing the table size.
Neither of those possibilities seem to be covered by your initial stated requirements ..
I would like to resize the table using VBA by extending the bottom of it to cover the number of rows added to in column G.

Does anyone know how I can modify the code so that it expands the number of rows the table covers, based in new row entries in column G
.. which specifically stated (twice) adding rows and only based on what was added in column G, so perhaps you can see why I asked the question. :)

Unless it is resolved beforehand, I will take another look at your revised question when I get a chance.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

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