I'm copying data using Advanced Filter, from one workbook table to a different workbook table. The copying works absolutely fine, but the output table does not resize the range to include the new data when it's entered. I've tried the code below (and other code) to get the table to resize the range, but it's not doing anything. It's not giving an error either, just running through the code.
This is the last part of the code that's supposed to resize the table:
With tblFiltered
.Resize .Range(myLastRow, tblFiltered.HeaderRowRange.Count)
End With
I'm including some more code, so you can see what the variables are set to.
'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 .Range(myLastRow, tblFiltered.HeaderRowRange.Count)
End With
I've tried using .currentregion
but that didn't do anything either.
I'll be using the above code for multiple tables, to copy and then resize and change every week. So hard coding the values for resizing is not an option.
The
myLastRow
variable calculates correctly to a value (number of the last row), as do the tblFiltered.HeaderRowRange.Count
(14 in this instance).
Not sure where I'm going wrong, if someone has any insight, that'll be helpful, thanks.
your issue is that you are giving the Resize
method of Table
object a Range
object that is not what you'd expect it to be
in fact,
.Range(myLastRow, tblFiltered.HeaderRowRange.Count)
is actually returing the cell myLastRow -1
rows and tblFiltered.HeaderRowRange.Count
columns off the referenced table first cell ...
Then, you should code as follows:
With tbl
.Resize .HeaderRowRange.Resize(myLastRow - .HeaderRowRange.Rows(1).row + 1)
End With
that is feeding the Resize
method of Table
object with a Range
object obtained as the referenced table header range resized as many rows as to reach your myLastRow
row