Results 1 to 4 of 4

Matching a cell ref. in a macro

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    02-09-2006
    Location
    Melbourne, Australia
    MS-Off Ver
    Office 2016
    Posts
    1,077

    Question Matching a cell ref. in a macro

    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.
    Attached Files Attached Files
    Trish in Oz
    -------------
    A problem well defined is a puzzle half solved


    If you attach a sample of your workbook it will be easier to find a solution. Remember to remove/replace sensitive data before uploading the file. Look here if you need help with attachments:
    http://www.excelforum.com/faq.php?fa...b3_attachments

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1