+ Reply to Thread
Results 1 to 19 of 19

Macro error help AutoFilter searching 2 columns

  1. #1
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Question Macro error help AutoFilter searching 2 columns

    Hello.

    I am using this AutoFilter macro I found from another site. I want to add a 2nd filter specifically for column 2. I have tried copying the code and making a new sub, adjusting the cell values to suit and the filterrange to 2, but I get "Complie error : ambiguous name detected : Worksheet_Change". I have tried renaming them but as soon as I rename the first one to anything different it stops working. Is it possible to combine the 2 search requirements into the 1 sub?

    Please Login or Register  to view this content.
    That works OK.

    This below does not.

    Please Login or Register  to view this content.

    Thank you.
    Last edited by rik130; 12-07-2019 at 10:02 PM.

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Small macro correction please

    .
    There can be only one "Private Sub Worksheet_Change(ByVal Target As Range)"

    Combine both macros into one :

    Please Login or Register  to view this content.

    ps: The above is a quick response. You may need to rename the variables in the second macro to be unique from those in the first macro or delete some of the variables like the DIM statements in the second macro.
    Last edited by Logit; 12-07-2019 at 07:08 PM.

  3. #3
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Small macro correction please

    Thanks Logit. Unfortunately that doesn't work.

    "Compile Error : Duplicate declaration in current scope."
    Last edited by rik130; 12-07-2019 at 07:19 PM.

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Small macro correction please

    .
    This works here BUT it will only filter one column at a time. You can filter Col A ... then if you want to filter Col B, you will need to unfilter Col A.

    Please Login or Register  to view this content.
    Note how the variables in the second macro are all named differently from the first macro. That is why you were getting the last error message.

  5. #5
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Small macro correction please

    Thanks Logit, unfortunately that doesn't work either.

    "Run-time error 1004. AutoFilter method of Range class failed" and highlights this section of the code :

    Please Login or Register  to view this content.
    Last edited by rik130; 12-07-2019 at 08:31 PM.

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Small macro correction please

    Hi rik30,

    Before we proceed with this thread, please read the forum rules about proper thread titles and using code tags. Thanks!
    HTH
    Regards, Jeff

  7. #7
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Small macro correction please

    I can't seem to attach the xlsm, when I click the 'attach' button I just get an empty white box even though I've uploaded it in the 'advanced' screen. I got the demo file from here if you want to look it to see what may be causing the error : teachexcel.com/excel-tutorial/2205/filter-data-as-you-type-in-excel?nav=yt .

    Jeff sorry for not using the code tag in my previous post, I've changed it now.

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Small macro error help

    Thank you rik130 for adding the code tags, but please do something to your thread title as well.

    Administrative Note:

    Welcome to the forum.

    We would love to help you with your query; however, the thread title does not really convey what your request is about.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)

  9. #9
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Macro error help AutoFilter searching 2 columns

    I've changed it twice already. I don't know how many more times you want me to change it? Perhaps you could change it to something that meets with your requirements as clearly my efforts are not good enough.

    Could someone answer my query please!

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    rik130 :

    Tested and works here. Apologies for the oversight ...

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Macro error help AutoFilter searching 2 columns

    Thanks logit, unfortunately that still doesn't work. Filter on column 1 works fine but when clearing it and entering a value from column 2 in B2, it hides all records, including the row you are filtering for.

  12. #12
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    Ok.

    What is the goal you are expecting ?

    It is late here now and I am off to sleep. Will check in the morning.

    Cheers.

  13. #13
    Registered User
    Join Date
    12-07-2019
    Location
    London
    MS-Off Ver
    Excel
    Posts
    7

    Re: Macro error help AutoFilter searching 2 columns

    As stated, there is no filtering on column 2. If the filter for column 1 is empty and you enter some search criteria for data in column 2, all it does is hides all rows, including any that match the filter criteria. In short, column 2 filter does nothing other than hide all records. Can column 2 be made to show records that match criteria entered in the filter?

  14. #14
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    If I enter "B8" for cell B2, then press ENTER, it displays cells B2, B4 and B8. The macro is performing as it is designed based on the
    current logic.

    If you edit this line : Set filterRange2 = Range("B4:B16") to this : Set filterRange2 = Range("B3:B16") then
    the only cells displayed (based on the example above), will be cells B2 and B8. Again, this is how the filter is designed to work.

    If this doesn't solve the issue, please provide additional information.

    Thanks.

  15. #15
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Macro error help AutoFilter searching 2 columns

    Place an example of your file.

  16. #16
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    Here is a copy of my workbook example for you to review. Does mine function the same as yours ?
    If not ... what differences are there ?
    Attached Files Attached Files

  17. #17
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Macro error help AutoFilter searching 2 columns

    @Logit, I ask the TS for an example file. With almost 5000 posts you can't come up with something better than the example?
    Please Login or Register  to view this content.
    Attached Files Attached Files

  18. #18
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    Vraag en antwoord :

    Once again, ignorance and insolence has raised its ugly head, then voiced a personal opinion which is unsolicited and unwarranted.


    "@Logit, I ask the TS for an example file." Yes you did ... but you presume too much. Perhaps with slight consideration on your part
    you might realize my workbook copy was provided for the OP to review ... irrespective of your request. Thank you for suggesting the OP post HIS
    workbook copy for review as that prompted me to provide my copy for him to review in hopes it would assist him.


    "With almost 5000 posts you can't come up with something better than the example?" And by what authority do you presume to judge
    any else's skills ?



    Let me enlighten you why I did not change the OP's original code. An OP is familiar with what they are doing ... not what someone else understands who
    has a different skill set. Rather than confuse an OP with something totally different I believe it is always best to provide them guidance and/or an answer
    based on their existing code ... if possible.

    In the past I learned early on when I completely changed an OP's original code the most often immediate response was "What was wrong with my code ?" So ...
    I stopped changing someone else's code if ... the code they provided worked with some tweaking.

    You as an additional contributor may always add to the OP's knowledge level by providing a different method. HOWEVER, when this is done it is ALWAYS best
    to provide the OP with a discussion / comments of what the new code is doing and why. Otherwise, what is the point ?

    Today's world is overrun with 'holier than thou' critics, nervous with anticipation of who their next victim will be. I for one am sick and tired of all of it and will
    call it out - for what it is - when it occurs - in as nice a manner as possible.

    Thank you for volunteering your efforts for the OP's benefit. You are however, not welcome to display your ignorance and holier mindset towards others.

    May you have a wonderful day and Merry Christmas / Happy New Year !

    Cheers.

  19. #19
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,009

    Re: Macro error help AutoFilter searching 2 columns

    .
    rik130 :

    May I offer you my sincere apologies for your experiences so far on this FORUM ? Please don't judge these occurrences as "the norm" of how things
    work here.

    All of this is an exception to the rule.

    Again, please accept my apologies.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Very Small Error In Uniques arrangement Code, Need correction
    By structo in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-16-2016, 05:58 AM
  2. Adding some correction to existing macro
    By Pilot5000 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-30-2015, 04:33 AM
  3. Need correction in macro
    By SHEIKH TALIB in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-23-2014, 12:55 PM
  4. [SOLVED] Correction of a couple of macro codes
    By aaaaaaiden in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2014, 06:08 AM
  5. SMALL(IF formula correction
    By always Stumped in forum Excel General
    Replies: 2
    Last Post: 06-08-2011, 03:23 PM
  6. Correction in Macro needed
    By maximpinto in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-29-2009, 07:23 AM
  7. [SOLVED] SUMPRODUCT - small correction required
    By Madiya in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-10-2006, 06:55 AM

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