captainxcel
New Member
- Joined
- Jul 28, 2017
- Messages
- 35
- Office Version
- 2016
- Platform
- 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
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