Solved

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

Posted on 2014-09-09
2
860 Views
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
    Range("A2").Select
        Else
   
    End If
   
   
   
    Range("A2").Select
   
    Dim LR As Long
   
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A2:T" & LR).SpecialCells(xlCellTypeVisible).Select
   

    Selection.Copy
    Sheets("Sheet1").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste

Appreciate some help.
Thank you
0
Comment
Question by:Jagwarman
2 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 400 total points
ID: 40312441
Hi,

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
Else
    myRange.EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
End Sub

Regards
0
 
LVL 45

Assisted Solution

by:aikimark
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

0

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