VBA Copy visible filtered data to another sheet

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
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.

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.
 
These two lines
Code:
Set src = ThisWorkbook.Sheets("OPEN TICKETS")    '************THIS KEEPS HIGHLIGHTING IN YELLOW*****************
    Set tgt = ThisWorkbook.Sheets("VIVINT CANADA INC 5590")
are looking for the 2 sheets within the workbook containing the macro. As they are not there, you need to replace ThisWorkbook with the name of the workbook.
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top