I've used exact same technique in the different module and worked like a charm. I've started new macro with the below code and it fails miserably as soon as trying to Select
all the filtered cells to make a copy into the template file. It both use Tables
so ListObjects
. What am I doing wrong here?
Sub ImportMasterData()
Dim MasterData As Workbook
Dim Template As Workbook
Dim DateFrom As Date
Dim DateTo As Date
DateFrom = #2/4/2021#
DateTo = #6/4/2021#
Set MasterData = Workbooks.Open("C:/Users/RJamborski/Desktop/VBA/Master Archive.xlsx")
Set Template = Workbooks.Open("C:/Users/RJamborski/Desktop/VBA/MasterArchiveTemplate.xlsx")
With MasterData.Sheets(1).ListObjects(1).DataBodyRange
.AutoFilter Field:=1, Criteria1:= _
Array("Dav1 - Danes Way", "Dav2 - Rail Port", "Dav4 - MagnaParkLutterworth"), _
Operator:=xlFilterValues
.AutoFilter Field:=4, Criteria1:=">=" & DateFrom, _
Operator:=xlAnd, Criteria2:="<=" & DateTo
End With
Debug.Print MasterData.Sheets(1).ListObjects(1).DataBodyRange.Rows.Count ' this prints 32106
MasterData.Sheets(1).ListObjects(1).DataBodyRange.SpecialCells(xlCellTypeVisible).Select ' but this throws an error
Selection.Copy
Template.Sheets(1).ListObjects(1).Range("A2").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub