I am just starting out with VBA and copied some macro code (copied from a post on this forum) that filters data and copy/pastes the result into another worksheet. As written and modified for my workbook it works really well, but I have some things I wish to change but can't work out how.
I am attaching a sample of the workbook (with names deleted).
1) I can only make the macro work if I hard code the criteria, but I want to reference cells on the Data worksheet.
2) I don't want to delete and replace the MyAutoFilter worksheet every time, but prefer to paste the results into the Summary worksheet starting in cell A10 and leaving one blank row between each resulting data set. (There are 3 separate pastes in the macro.)
This is the section of code I need to modify to reference a particular cell. Except for the capitalized comment, all other comment rows are from the original macro author. (I tried using Criteria1:="=" & WS.Range("D4").Value but got an error on running the macro.)
'This example filters on the column in the range
'In this case the range starts in B so Field:=1 is column B, etc
'FILTERS ON % UNDER BUDGET (cell D4) the cell reference I want to use for the autofilter
rng.AutoFilter Field:=2, Criteria1:="<-0.1" the hard coded criterion
'if you want to filter on a cell value you can use this, use "<>" for the opposite
'This example uses the activecell value
'rng.AutoFilter Field:=1, Criteria1:="=" & ActiveCell.Value
'This will use the cell value from A2
'rng.AutoFilter Field:=1, Criteria1:="=" & WS.Range("A2").Value
I can see lots of benefits in learning about macro VBA and would really appreciate any help I can get with this.
Bookmarks