+ Reply to Thread
Results 1 to 13 of 13

VBA Filter Using Named Ranges as Criteria

  1. #1
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    VBA Filter Using Named Ranges as Criteria

    I am trying to filter data by named range critera. For instance, in column B, cells fall into various named ranges (B2, B10, and B12 belong to named range "FirstNamedRange"; B4, B10, and B8 belong to "SecondNamedRange"; and B22, B23, and B25 belong to "ThirdNamedRange"). As you can see from these examples, not all cells in column B are assigned to a particular named range, and cell B10 belongs to both FirstNamed Range and SecondNamedRange.

    I want to filter by a specified named ranges but cannot figure out how to indicate a named range as the criteria. Below are some codes that I have tried with no success - mismatch errors, no critera pulled, etc. To give you a general idea of my plans for this piece of code, I am going to use it to filter and show all cells belonging to FirstNamedRange and then I will use it to build a code to filter and show all cells belonging to FirstNamedRange OR SecondNamedRange, then FirstNamedRange AND SecondNamedRange....That kind of idea.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Thank you in advance for any help you can provide. Any forum threads I have found on the internet on this subject have proven unhelpful.
    Last edited by Mediterranean; 04-25-2013 at 02:44 PM. Reason: Added code

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: VBA Filter Using Named Ranges as Criteria

    Attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  3. #3
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    Per your request, here are example files of information unfiltered, then filterd by "FirstNamedRange OR SecondNamedRange", then filtered "FirstNamedRange AND SecondNamedRange". Thank you very much for your help.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Filter Using Named Ranges as Criteria

    Try this line

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    Unfortunately, I am getting a Type Mismatch error now. I have attached the spreadsheet example that I used your code in.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Filter Using Named Ranges as Criteria

    You have this line =Sheet2!$B$2,Sheet2!$B$10,Sheet2!$B$12
    in firstname range. Is this right?

  7. #7
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    That is correct. Different cells in the same column belong to different named ranges

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Filter Using Named Ranges as Criteria

    CLng converts in to integer and the columns in B are text

  9. #9
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    So do I need to use CStr instead?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Filter Using Named Ranges as Criteria

    I don't think you can't use a non-contigous range to filter by, all your ranges are non-contiguous.

    Also, none of them include the header.

    Perhaps you should copy the criteria to another range and then filter by that.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  11. #11
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    Wonderful! It worked really well. Would you mind explaining to me what the following is doing/describing: ".Item(1).Resize(rCrit.Cells.Count + 1)"

    And one more question, which range is "UsedRange" referring to? Is it considering L2 through L4 to be the last used range?

    Thank you both very much for all your help!

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Filter Using Named Ranges as Criteria

    The UsedRange part is your code which I didn't touch, it's referring to the intersection between column B on the worksheet and the used range on the worksheet.

    Item(1) refers to Range("L1") and probably isn't needed now that I think about it, Rezise should work on it's own.
    Last edited by Norie; 04-29-2013 at 04:35 PM.

  13. #13
    Registered User
    Join Date
    02-11-2013
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: VBA Filter Using Named Ranges as Criteria

    Oh, that's right. You were using a variation of my first code that I posted. Thank you for your explanation of the code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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