Resize Tables using Table Names from Col A and the new Row count from Corresponding Col B

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
585
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to use a macro I found here in this group, but with a slight modification. I have multiple tables on the same worksheet called "Drops" that I want to resize with the same macro. Column A2:A14 consist of the worksheets table names and column B2:B14 consist of the corresponding number of rows that I want to resize all of these tables to. I've added a variable to capture the table name from column A, using the variable named "tbName1". However, I'm having trouble replacing "Table1" in the code with "tbName1". Any assistance would be greatly appreciated.


Thanks, SS


VBA Code:
Sub ResizeTables()

Dim rng1 As Range
Dim tb1 As ListObject
Dim tbName1 As String

    tbName1 = Range("A8")
        
    Set rng1 = Range("Table1[#All]").Resize(Range("B8").Value)
        
    Set tb1 = ActiveSheet.ListObjects("Table1")
    tb1.Resize rng1

End Sub
 
could you explain to me why that shouldn't still work?
If you do that the code will "work" in that it will not error. However, it would not resize a table under some circumstances.
If you want to move that bit of code it would be safest to put the End If up a little further like this

Rich (BB code):
Sub Clear_And_Resize_Tables_v2()
  Dim c As Range
  
  For Each c In Range("A2:A14")
    With Sheets("Drops").ListObjects(c.Value)
      If WorksheetFunction.CountA(.Range) > .Range.Columns.Count Then
        .DataBodyRange.ClearContents
      End If
      .Resize (.Range.Resize(c.Offset(, 1).Value))
    End With
  Next c
End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
If you do that the code will "work" in that it will not error. However, it would not resize a table under some circumstances.
If you want to move that bit of code it would be safest to put the End If up a little further like this

Rich (BB code):
Sub Clear_And_Resize_Tables_v2()
  Dim c As Range
 
  For Each c In Range("A2:A14")
    With Sheets("Drops").ListObjects(c.Value)
      If WorksheetFunction.CountA(.Range) > .Range.Columns.Count Then
        .DataBodyRange.ClearContents
      End If
      .Resize (.Range.Resize(c.Offset(, 1).Value))
    End With
  Next c
End Sub
Thanks. Not sure I understand it 100%, but will go back try this out as well.
 
Upvote 0
Not sure I understand it 100%,
Well, let's suppose that you had the code as you originally changed it - to this I believe
VBA Code:
Sub Clear_And_Resize_Tables_v2()
  Dim c As Range
  
  For Each c In Range("A2:A14")
    With Sheets("Drops").ListObjects(c.Value)
      If WorksheetFunction.CountA(.Range) > .Range.Columns.Count Then
        .DataBodyRange.ClearContents
        .Resize (.Range.Resize(c.Offset(, 1).Value))
      End If
    End With
  Next c
End Sub

If you had data in one of the tables and column B said to resize that table to 5 rows and ran the code it would clear the data from the table and resize it to 5 rows as desired.
However, if you then decided that you actually wanted that table say 7 rows so changed the relevant column B value to 7 and ran the code again, then that table would not be resized from 5 rows to 7 rows.

With the code in post #11 though, the table would be resized from 5 rows to 7.
 
Upvote 0
Well, let's suppose that you had the code as you originally changed it - to this I believe
VBA Code:
Sub Clear_And_Resize_Tables_v2()
  Dim c As Range
 
  For Each c In Range("A2:A14")
    With Sheets("Drops").ListObjects(c.Value)
      If WorksheetFunction.CountA(.Range) > .Range.Columns.Count Then
        .DataBodyRange.ClearContents
        .Resize (.Range.Resize(c.Offset(, 1).Value))
      End If
    End With
  Next c
End Sub

If you had data in one of the tables and column B said to resize that table to 5 rows and ran the code it would clear the data from the table and resize it to 5 rows as desired.
However, if you then decided that you actually wanted that table say 7 rows so changed the relevant column B value to 7 and ran the code again, then that table would not be resized from 5 rows to 7 rows.

With the code in post #11 though, the table would be resized from 5 rows to 7.
Thanks. Makes more sense now.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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