VBA Code that will filter and copy but not if there is no data

Posted on 2014-09-09
Last Modified: 2014-09-09
I need to filer Row 1 Column 'T' to find items that are 'New' When filtered I need to copy the items [but not the header] and paste these onto a new sheet, but if wheh I filter there is no data I do not want to copy anything.

I was trying to use the below code but if there is no data it copies the header

ActiveSheet.Range("$A:$T").AutoFilter Field:=20, Criteria1:="<>"
    If ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
    End If
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:T" & LR).SpecialCells(xlCellTypeVisible).Select

    ActiveCell.Offset(1, 0).Select

Appreciate some help.
Thank you
Question by:Jagwarman
LVL 49

Accepted Solution

Rgonzo1971 earned 400 total points
ID: 40312441

pls try

Sub macro()
Dim myRange
ActiveSheet.Range("$A:$T").AutoFilter Field:=20, Criteria1:="<>"
Set myRange = Range(Range("T2"), Range("T" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
If myRange.Row = 1 Then ' no data
    Exit Sub
    myRange.EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
End Sub

LVL 45

Assisted Solution

aikimark earned 100 total points
ID: 40312736
Try this:
If WorksheetFunction.CountIf(activesheet.Range(Range("T2"), Range("T" & Rows.Count).End(xlUp)),"new") <> 0 Then
'place your filtering code here

End If

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
macro for finding text and replacing 7 43
Help with excell ... 6 61
Hlookup formula help 14 19
Easy Excel formula needed 4 26
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now