I want to loop through tables in a workbook and rename certain column headers in tables, to enable the Advanced Filter to copy data. Currently, I'm using On Error Resume Next
to avoid Error messages when the column isn't found in the table, and then move on to the next table.
Although this method works absolutely fine, it created problems further down the code when I tried to resize the range of the table. The resizing just didn't work. With help from @HTH, it became apparent that the On Error Resume Next
was the problem after some code changes.
Is there a way to fix the On Error Resume Next
or should I use a different method to loop through the tables and rename the headings, skipping the tables that don't have those specific headings?
Current Relevant code:
'Loop through and apply a change to all Tables in the Excel Workbook
Dim tbl As ListObject
Dim sht As Worksheet
'Loop through each sheet and table in the workbook
For Each sht In wb.Worksheets
For Each tbl In sht.ListObjects
On Error Resume Next
'rename headings
tbl.ListColumns("Ranging").Name = "MS"
tbl.ListColumns("Stock on Hand - Store").Name = "SOH"
Next tbl
Next sht
'Create Filter Criteria ranges
With MainWB.Worksheets.Add
.Name = "FltrCrit"
Dim FltrCrit As Worksheet
Set FltrCrit = MainWB.Worksheets("FltrCrit")
End With
With FltrCrit
Dim DerangedCrit As Range
Dim DormantCrit As Range
Dim OverstockCrit As Range
Dim OutdatedCrit As Range
Dim NegCrit As Range
Dim myLastColumn As Long
'Create Deranged Filter Criteria Range
.Cells(1, "A") = "Deranged"
.Cells(2, "A") = "MS"
.Cells(3, "A") = "<>4"
.Cells(2, "B") = "SOH"
.Cells(3, "B") = "=0"
'get last column, set range name
With .Cells
'find last column of data cell range
myLastColumn = .Find(What:="*", After:=.Cells(2), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column
'specify cell range
Set DerangedCrit = .Range(.Cells(2, "A:A"), .Cells(3, myLastColumn))
End With
End With
'Copy Filtered data to specified tables
Dim tblFiltered As ListObject
Dim copyToRng As Range, SDCRange As Range
'DERANGED
'Store Filtered table in variable
Set tblFiltered = wb.Worksheets("Deranged with SOH").ListObjects("Table_Deranged_with_SOH")
'Remove Filtered table Filters
tblFiltered.AutoFilter.ShowAllData
'Set Copy to range on Filtered sheet table
Set copyToRng = tblFiltered.HeaderRowRange
Set SDCRange = MainWB.Worksheets(2).ListObjects("Table_SDCdata").Range
'Use Advanced Filter
SDCRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=DerangedCrit, CopyToRange:=copyToRng, Unique:=False
'Resize filtered table to include new data
With wb.Worksheets("Deranged with SOH").Cells
'find last row of source data cell range
myLastRow = .Find(What:="*", LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
With tblFiltered
.Resize .HeaderRowRange.Resize(myLastRow - .HeaderRowRange.Rows(1).Row + 1)
End With
'Clear filter data on SDC
MainWB.Worksheets(2).ListObjects("Table_SDCdata").AutoFilter.ShowAllData