Object Error - VBA

captainxcel

New Member
Joined
Jul 28, 2017
Messages
35
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone - Thanks in advance.
I'm trying to create a macro to resize 20 tables. The table names are in cells and the macro creates an array of these and then starts a resize loop. I'm getting an object error at: Set tableName = Codes(i). Any ideas of what I need to do here?
Thanks!

Sub resizeClientTables()
Dim sh As Worksheet
Set sh1 = ThisWorkbook.Worksheets("Lists")

Dim Codes(1 To 20) As String

'Create Array
Dim i As Long
For i = 1 To 20
Codes(i) = sh1.Range("A3").Offset(i).Value
Next i

'Define Vars
Dim sh2 As Worksheet
Dim tableName As String
Dim loTable As ListObject
Dim loRows As Integer, loColumns As Integer
Dim iNewRows As Integer, iNewColumns As Integer

'Get number of rows to add or delete
iNewRows = Application.InputBox("Enter a positive or negative amount of rows:", Type:=1)
iNewColumns = 0

'Cycle through tables and resize
For i = LBound(Codes) To UBound(Codes)
Set tableName = Codes(i)
Set sh2 = Sheets("Rebalance")
Set loTable = sh2.ListObjects(tableName)
loRows = loTable.Range.Rows.Count
loColumns = loTable.Tange.Columns.Count

loTable.Resize loTable.Range.Resize(loRows + iNewRows, loColumns + iNewColumns)
Next i

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You don't Set strings, only objects, so it should be
VBA Code:
tableName = Codes(i)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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