Solved

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

Posted on 2014-09-09
2
1,078 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 51

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

687 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