Excel Macro to filter/paste data

Tuxracer

New Member
Joined
Sep 7, 2017
Messages
4
Hello, I have searched and found examples closely related to what I am trying to do, but still need help. I have recorded a macro of the desired process, shown below.

AutoFilter Field:=1, Criteria1: will vary based on what the user populates in cell Sheet1!B1
AutoFilter Field:=1, Criteria1: will vary based on what the user populates in cell Sheet1!D2

I need to filter Sheet2 based on what the user specifies in those two cells on Sheet1, copy the filtered data from Sheet2 and then paste special values back into Sheet1

Sheet2 has approximately 10,000 rows of data to be filtered.

Code:
Sub Macro2()
'
' Macro2 Macro
'


'
    Sheets("Sheet2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=1, Criteria1:="123"
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=2, Criteria1:="456"
    Range("AF17:AH22").Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A12").Select
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Sorry one edit to the above post.

AutoFilter Field:=1, Criteria1: will vary based on what the user populates in cell Sheet1!B1
AutoFilter Field:=2, Criteria1: will vary based on what the user populates in cell Sheet1!D2
 
Upvote 0
Hi & welcome to the board
Try
Code:
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=1, Criteria1:=Sheets("Sheet1").Range("B1").value
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=2, Criteria1:=Sheets("Sheet1").Range("D2").value
 
Upvote 0
Hi, thank you for the quick reply and the welcome :)

I tried replacing my two AutoFilter lines with your code. How can I copy the resulting filtered rows? I still have my old range defined

Sorry I'm pretty new to macros and vba but trying to learn :)

Code:
Range("AF17:AH22").Select
 
Upvote 0
This assumes that your data starts in row 2 & that you don't want to copy the header row
Code:
Sub Macro2()
'
' Macro2 Macro
'


'
    Sheets("Sheet2").Select
    Selection.AutoFilter
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=1, Criteria1:=Sheets("sheet1").Range("B1")
    ActiveSheet.Range("$AD$1:$AO$10222").AutoFilter Field:=2, Criteria1:=Sheets("sheet1").Range("D2")
    Range("AF2:AH10222").SpecialCells(xlVisible).Copy
    Sheets("Sheet1").Select
    Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("A12").Select
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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