Autofilter Multiple Criteria, Copy and Paste Visible cells only excluding header

scapaflow

New Member
Joined
Feb 13, 2013
Messages
4
Requesting help with setting up a macro to autofilter multiple criteria, copying visible data only, and selecting and pasting sections of the data at a time into another worksheet in the file.


Here is a previous post that works for single criteria:


https://www.mrexcel.com/forum/excel...le-cells-after-auto-filter-except-header.html




I am using the code below and getting this error


"Run-time error '1004':
Application-defined or object-defined error"




Sub Filter()
Dim DateToday As Date
DateToday = Date$
Application.ScreenUpdating = True
Dim Lastcolumn As Long
Dim CopySheet As String
Dim PastSheet As String
CopySheet = "Absent_General"
PasteSheet = "Absent"
Lastcolumn = Sheets(CopySheet).Cells(1, Columns.Count).End(xlToLeft).Column
Lastrow = Sheets(PasteSheet).Cells(Rows.Count, "A").End(xlUp).Row + 1


With Sheets(CopySheet).Range(Cells(1, 1), Cells(Cells(Rows.Count, "A").End(xlUp).Row, Lastcolumn))
.AutoFilter Field:=13, Criteria1:="Absent"
.AutoFilter Field:=14, Criteria1:=DateToday
.Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).copy Sheets(PasteSheet).Cells(Lastrow, 1)
End With
Sheets(CopySheet).AutoFilterMode = False
Application.ScreenUpdating = True
End Sub


I am using criteria DateToday as a variable assigned with today's date. I need to search the data on the first worksheet named 'Absent_General' and paste it into the 'Absent' worksheet. In the original code, it only used 1 criteria for filtering, copying the sections, and pasting. I need to add 2 criteria with the code:


.AutoFilter Field:=13, Criteria1:="Absent"
.AutoFilter Field:=14, Criteria1:=DateToday


And its not working. Any help / advice would be greatly appreciated!
 

Some videos you may like

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,095
Office Version
2007
Platform
Windows
Try the following:

(Do not put DateToday = Date$ only DateToday = Date)

Code:
Sub Filter()
    Dim DateToday As Date
    Dim Lastcolumn As Long, u as Long
    Dim CopySheet As String
    Dim PastSheet As String
    
    Application.ScreenUpdating = False
    
    CopySheet = "Absent_General"
    PasteSheet = "Absent"
[COLOR=#0000ff]    DateToday = Date[/COLOR]
    
[COLOR=#0000ff]    If Sheets(CopySheet).AutoFilterMode Then Sheets(CopySheet).AutoFilterMode = False[/COLOR]
    Lastcolumn = Sheets(CopySheet).Cells(1, Columns.Count).End(xlToLeft).Column
    Lastrow = Sheets(PasteSheet).Cells(Rows.Count, "A").End(xlUp).Row + 1
    
    With Sheets(CopySheet).Range(Cells(1, 1), Cells(Cells(Rows.Count, "A").End(xlUp).Row, Lastcolumn))
        .AutoFilter Field:=13, Criteria1:="Absent"
[COLOR=#0000ff]        .AutoFilter Field:=14, Operator:=xlFilterValues, Criteria2:=Array(2, Format(DateToday, "mm/dd/yyyy"))[/COLOR]
        u = Range("N" & Rows.Count).End(xlUp).Row
        If u = 1 Then
            MsgBox "There is no data to copy"
        Else
            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Copy Sheets(PasteSheet).Cells(Lastrow, 1)
            MsgBox "Copied data "
        End If
    End With
    If Sheets(CopySheet).AutoFilterMode Then Sheets(CopySheet).AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,101,754
Messages
5,482,680
Members
407,357
Latest member
Chazs

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top