+ Reply to Thread
Results 1 to 4 of 4

Can't make autofilter find the number "1" and not "10"

  1. #1
    Registered User
    Join Date
    09-24-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    3

    Can't make autofilter find the number "1" and not "10"

    Hi experts,

    I have a column with data, which is controlled by autofilter in VBA. As an exampel, some of the rows contains thees data:

    Column H:
    10
    1,3,7,10
    'empty
    3,7,10

    (all in same column - H!)

    If I use the following code, all four lines returns (month=1)

    Please Login or Register  to view this content.
    The problem is, that "1" is also in the number "10"

    I hope you understand my qustion, 'cause it's a little difficult to explain
    Last edited by PBChristensen; 09-25-2012 at 08:23 AM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Can't make autofilter find the number "1" and not "10"

    Welcome to the Forum!

    To look at the big picture, you are probably inviting trouble by put multiple numeric values into a single cell. This causes them to be managed as a string. And as you have found, a 1 contained in a string could be part of 1, 10, 11, 12, etc.

    One modification that will cut down on false positives is to add a comma after the search. But you will then miss a 1 if it is the last number in a sequence, or the only number in the cell.

    Please Login or Register  to view this content.
    Excel's built-in pattern matching is a little limited. A more complex method would be to write your own filtering Sub that looks at every line and uses a more complex pattern matching approach, and then hide any row that doesn't match. I would do that with regexp (see implementation of utilities at http://www.tmehta.com/regexp/add_code.htm and a reference for regexp at http://www.regular-expressions.info/). You would then hide any row that does not match the pattern

    month & "[^,$]"

    (I have not tested this.)

    However, I think the best way to do this is to redesign how you are recording your data. I can't give any advice on that without knowing more about what you are doing. Can you attach a sample workbook?
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    09-24-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Can't make autofilter find the number "1" and not "10"

    Hi,

    Thank for your reply.

    The data is recorded in a userform in another workbook, and is therefore not an option.

    And regarding your other suggestion, adding a comma, I've also tried, but when I also need the blank rows, it gives me three criterias, which I cannot get to work.

    I've tried this, with these data:

    10.
    1.3.7.10.
    'empty
    3.7.10.

    Please Login or Register  to view this content.
    ...but then 11. is a problem. (and 2. vs. 12.!?!?!)

  4. #4
    Registered User
    Join Date
    09-24-2012
    Location
    DK
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Can't make autofilter find the number "1" and not "10"

    I solved the problem, by making a fake column, which converted the string in column H into another string in column AE- which was able to be filtered!

    Please Login or Register  to view this content.
    and then include these codes in VBA:
    Please Login or Register  to view this content.
    Thank you for your time!!!

+ 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