Solved

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

Posted on 2014-09-09
2
815 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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

706 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

16 Experts available now in Live!

Get 1:1 Help Now