You sent me a private message about my comment regarding protection above.
You're quite right, I don't need to unprotect that sheet to write to it.
There were several things that went wrong when I opened your file; missing references to
Common Controls, which in turn fouled up simple vba functions such as
Date, overflow errors because you've removed all data, so lines such as:
Template_row = Range("AN39", Range("AN39").End(xlDown)).Rows.Count
return large numbers that the
Integer type variable
Template_row can't hold, a missing sheet
Completed which caused a subscript error, the code hanging needing me to force close Excel because it might have got itself into a long or never-ending loop.
All of this caused me to give up, whereas really I should have persevered and focused on the individual line of code.
You can do something like this:
Code:
'Sheets("Workings").Activate 'not needed for this
DestnRow = Sheets("Template").Cells(Rows.Count, "J").End(xlUp).Row + 1 'blank row in column J. Can use later for other column-copying so that they align properly
Set WorkingsTable = Sheets("Workings").ListObjects("Table1") 'easier to set a variable to the list object for later code
WorkingsTable.Range.AutoFilter Field:=34, Criteria1:="=*" & StrAP & "*"
WorkingsTable.ListColumns("Invoice Amount (GST Excl)").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "J")
' WorkingsTable.ListColumns("Another Column Name Here").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "D??") 'for further column copying.
' WorkingsTable.ListColumns("Yet Another Column Name Here").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "F??")
or using the
With…End With construct:
Code:
DestnRow = Sheets("Template").Cells(Rows.Count, "J").End(xlUp).Row + 1 'blank row in column J. Can use later for other column-copying so that they align properly
With Sheets("Workings").ListObjects("Table1")
.Range.AutoFilter Field:=34, Criteria1:="=*" & StrAP & "*"
.ListColumns("Invoice Amount (GST Excl)").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "J")
' .ListColumns("Another Column Name Here").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "D??") 'for further column copying.
' .ListColumns("Yet Another Column Name Here").DataBodyRange.Copy Sheets("Template").Cells(DestnRow, "F??")
End With
Marc L is quite right that
range.copy works with visible cells and the above code should work as expected. However I have found sometimes, especially when you might
Intersect or
Union with range that it includes hidden cells too - I haven't worked out exactly when this happens so I often throw in the
.specialcells(xlvisible) property too:
Code:
WorkingsTable.ListColumns("Invoice Amount (GST Excl)").DataBodyRange.SpecialCells(xlVisible).Copy Sheets("Template").Cells(DestnRow, "J")
Then I'm certain it'll do what I want.