Hello, I found this code to filter data and then copy the visible results to anther sheet. The code "looks" like it should work fine but I keep getting an error saying "subscript out of range" for the src sheet. The workbook is a .csv file, I don't think this should matter though.
Any idea why this happening? This is the most basic process, just filter on one word and copy the results to another sheet.
Code:
Sub CopyPartOfFilteredRange()
Dim src As Worksheet
Dim tgt As Worksheet
Dim filterRange As Range
Dim copyRange As Range
Dim lastRow As Long
Set src = ThisWorkbook.Sheets("OPEN TICKETS") '************THIS KEEPS HIGHLIGHTING IN YELLOW*****************
Set tgt = ThisWorkbook.Sheets("VIVINT CANADA INC 5590")
' turn off any autofilters that are already set
src.AutoFilterMode = False
' find the last row with data in column A
lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
' the range that we are auto-filtering (all columns)
Set filterRange = src.Range("A1:P" & lastRow)
' the range we want to copy (only columns we want to copy)
' in this case we are copying country from column A
' we set the range to start in row 2 to prevent copying the header
Set copyRange = src.Range("A1:P" & lastRow)
' filter range based on column B
filterRange.AutoFilter field:=2, Criteria1:="VIVINT CANADA INC 5590"
' copy the visible cells to our target range
' note that you can easily find the last populated row on this sheet
' if you don't want to over-write your previous results
copyRange.SpecialCells(xlCellTypeVisible).Copy tgt.Range("A1")
End Sub
Any idea why this happening? This is the most basic process, just filter on one word and copy the results to another sheet.