Auto filter copy macro - date based data

chrislp

New Member
Joined
Mar 27, 2009
Messages
32
I'm a VB novice and am struggling to find the right code to copy, from one workbook, and paste into another, a defined range (only 15 rows) of auto filtered data, based on it being within a defined date range (I have a forumula which gives me a marker (1) for filtering. The row data changes according to the date and I want to copy no more than 15 rows of the filtered data. How would I code this to make the copy of the filtered selection within a defined range (B3:M250)? This is part of the code I currently have:

Code:
Windows("3.Period 2_Milestone_matrix.xls").Activate
    Sheets("All Milestones").Select
    Selection.AutoFilter Field:=9, Criteria1:="=CER", Operator:=xlAnd
    Selection.AutoFilter Field:=18, Criteria1:="=1", Operator:=xlAnd
    Range("B3:M250").Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Highlight report template.xls").Activate
    ActiveSheet.Paste

Hope this is clear, many thanks.
 
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Welcome to MrExcel board...

try this

Code:
Windows("3.Period 2_Milestone_matrix.xls").Activate
Sheets("All Milestones").Select
Selection.AutoFilter Field:=9, Criteria1:="=CER", Operator:=xlAnd
Selection.AutoFilter Field:=18, Criteria1:="=1", Operator:=xlAnd
LR = Cells(65536, 2).End(xlUp).Row
    If LR > 1 Then
        Range("B3:M" & LR).SpecialCells(xlCellTypeVisible).Copy
    End If
Windows("Highlight report template.xls").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
 
Upvote 0
Tried the above code and all is working fine when there are filter results. When there are no filter results however the copy seems to take the header row in row 2 and paste that. I'm not sure why it is selecting row 2 to copy, any ideas? Many thanks, here is the piece of code...

Sheets("ESRP All Milestones").Select
Selection.AutoFilter Field:=9, Criteria1:="=P&L", Operator:=xlOr, _
Criteria2:="=MCI"
Selection.AutoFilter Field:=18, Criteria1:="=1", Operator:=xlAnd
LR = Cells(65536, 3).End(xlUp).Row
If LR > 1 Then
Range("B3:M" & LR).SpecialCells(xlCellTypeVisible).Copy
End If
Windows("Highlight report template.xls").Activate
ActiveSheet.Paste
 
Upvote 0
so your headers are in row 2? If so then change this

If LR > 1 Then
to this
If LR > 2 Then
 
Upvote 0
Perfect, it doesn't copy the header row, what is does do though is crash when it tries to paste and it hasn't copied any data. Is there a way of fixing this? Thanks
 
Upvote 0
move the end if

Windows("3.Period 2_Milestone_matrix.xls").Activate
Sheets("All Milestones").Select
Selection.AutoFilter Field:=9, Criteria1:="=CER", Operator:=xlAnd
Selection.AutoFilter Field:=18, Criteria1:="=1", Operator:=xlAnd
LR = Cells(65536, 2).End(xlUp).Row
If LR > 1 Then
Range("B3:M" & LR).SpecialCells(xlCellTypeVisible).Copy
Windows("Highlight report template.xls").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
End If
 
Upvote 0

Forum statistics

Threads
1,214,667
Messages
6,120,820
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top