Filter for selected options and create a new sheet VBA Macro Reader

beginvbaanalyst

Board Regular
Joined
Jan 28, 2020
Messages
141
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I created a macro using the Macro Recorder but wanted to see if there's a much easier way to filter and paste a new sheet with less code.
I want it to filter for anything within the column and not a selected range ("$I$1:$M$83").
Would I use something like to select anything within the column:
Range("I2", Range ("I" & Rows.Count).End(xlUp)

Essiently I want to end up look up anything that has a 0 within my J Column.
1580916869782.png


My Macro Recorder Code is:
Sub TestFilter()
'
' TestFilter Macro
' EinaFilter
'

'
Range("I1:M1").Select
Selection.AutoFilter
ActiveSheet.Range("$I$1:$M$83").AutoFilter Field:=2, Criteria1:="1"
ActiveSheet.Range("$I$1:$M$83").AutoFilter Field:=3, Criteria1:="0"
Sheets("Sheet1").Select
Sheets("Sheet1").Copy After:=Sheets(1)
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:

VBA Code:
Sub TestFilter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With Range("I1:M" & Range("J" & Rows.Count).End(xlUp))
    .AutoFilter Field:=2, Criteria1:="1"
    .AutoFilter Field:=3, Criteria1:="0"
  End With
  Sheets("Sheet1").Copy After:=Sheets(1)
End Sub

If you only want to paste the filtered data in a new sheet, then:

VBA Code:
Sub TestFilter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With Range("I1:M" & Range("J" & Rows.Count).End(xlUp))
    .AutoFilter Field:=2, Criteria1:="1"
    .AutoFilter Field:=3, Criteria1:="0"
    ActiveSheet.AutoFilter.Range.EntireRow.Copy
    Sheets.Add after:=Sheets(1)
    ActiveSheet.Paste
  End With
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub TestFilter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With Range("I1:M" & Range("J" & Rows.Count).End(xlUp))
    .AutoFilter Field:=2, Criteria1:="1"
    .AutoFilter Field:=3, Criteria1:="0"
  End With
  Sheets("Sheet1").Copy After:=Sheets(1)
End Sub

If you only want to paste the filtered data in a new sheet, then:

VBA Code:
Sub TestFilter()
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  With Range("I1:M" & Range("J" & Rows.Count).End(xlUp))
    .AutoFilter Field:=2, Criteria1:="1"
    .AutoFilter Field:=3, Criteria1:="0"
    ActiveSheet.AutoFilter.Range.EntireRow.Copy
    Sheets.Add after:=Sheets(1)
    ActiveSheet.Paste
  End With
End Sub

Thank you for the response, I keep getting a compile error: Named argument not found
Is that because the second line of Criteria1 doesn't have any zeros?
I tried replacing with
.AutoFilter Field:=3, Criteria1:="1"
I know that in this column all fields are 1 and it keeps displaying the "named argument not found".
Can't wrap my head around why
 
Upvote 0
Did you modify something in the code?
Did you copy it full?
If there is no data, it simply does not filter anything, but does not send an error.

Thank you for the response, I keep getting a compile error: Named argument not found

On which line does the macro stop?
 
Upvote 0
Did you modify something in the code?
Did you copy it full?
If there is no data, it simply does not filter anything, but does not send an error.



On which line does the macro stop?

I was able to get it to work.
My problem is the If Active.Sheet.AutoFilterMode then ActiveSheete.AutoFilter Mode = False
This wouldn't let me run the code at all and I had to remove it.
Should I set this up as a new breakpoint by itself after I create a new sheet with the copied filter data?
 
Upvote 0
If Active.Sheet.AutoFilterMode then ActiveSheete.AutoFilter Mode = False

It is not written correctly, it should be like this:

VBA Code:
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

Just copy the line and paste in your macro.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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