+ Reply to Thread
Results 1 to 9 of 9

VBA macro to filter rows based on text entered in query cell

  1. #1
    Forum Contributor
    Join Date
    06-08-2012
    Location
    Panama City, FL
    MS-Off Ver
    Excel 2016
    Posts
    175

    VBA macro to filter rows based on text entered in query cell

    Hello Excel friends!

    Please look at the attached sample spreadsheet and explain to me why I cannot select (filter) only the rows that have a cell containing the partial data in the Query Box (C2) also know as "NameSearch".

    E1:H16 is my raw data.
    E20:H20 is my search criteria which appears once data is entered in C2, such as "reg". The criteria appears with asterisks on each side so partial data can be entered.

    The problem is, no matter what I enter in C2, all data is transferred to RESULTS.

    Thanks for any assistance!

    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! Try this !


    • For only a 'contains' criteria it can be achieved with only the cell C2 and the VBA function Filter …

    • As using an advanced filter you forgot to fill the criteria cells E20:H20 ‼

      Done with the so obvious easy formula =NameSearch then the Clear Cells button is useless …

      So to filter column by column a starter demonstration to paste to the Sheet1 worksheet module :

      PHP Code: 
      Private Sub Worksheet_Change(ByVal Target As Range)
                      
      Dim C&
          If 
      Target(1).Address = [NameSearch].Address Then
                  Application
      .EnableEvents False
              
      If IsEmpty(Target(1)) Then
                  
      [J3:M17].ClearContents
              
      Else
                  
      With [E1:H16].Columns
                      
      For 1 To .Count
                         
      .Item(C).AdvancedFilter xlFilterCopyCells(19C).Resize(2), Cells(2C)
                      
      Next
                  End With
              End 
      If
                  
      Application.EnableEvents True
          End 
      If
      End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 12-12-2019 at 10:03 PM. Reason: optimization …

  3. #3
    Forum Contributor
    Join Date
    06-08-2012
    Location
    Panama City, FL
    MS-Off Ver
    Excel 2016
    Posts
    175

    Re: VBA macro to filter rows based on text entered in query cell

    Bonjour Marc L, merci beaucoup pour votre rιponse.
    I'm not sure that you actually opened my attached spreadsheet and tried it, so I'd like to clarify for you or anyone else looking at this what we have here.
    It is very likely that I'm confused by your response, so pardon me if so.
    You referred to "=NameSearch" as a formula. Actually, and I probably shouldn't have done it, "NameSearch" is merely the name I gave to the cell "C2", because that's where I type in the partial text that I use to search through the names.
    You stated that I "forgot to fill the criteria cells E20:H22" and I assume that to mean that is the (or one of many) reason(s) that it doesn't work. But actually, if you type any text in cell C2 you will see that it immediately fills in cells E20:H20), each surrounded by asterisks for obvious reasons. The CLEAR CELLS button is to clear the C2, CRITERIA and RESULTS cells to prepare for my next query.

    So, given all that, I went in and deleted my three macros and replaced it with the one you so kindly created to accomplish my task.
    Obviously yours is much shorter and more direct and obviously I'm pretty new at VBA, but I presently cannot get it to work as is. While I see you were able to create the script without using the Criteria cells I placed on the sheet, the result is the same as before. It still lists all the names in E2:H16 to the Results.
    Is there any chance you might consider taking my spreadsheet and replacing my macros with yours and attaching it so that I (and others learning from this) can get it?
    If we type in the text "hard", for example, the result should show five rows of names, each row containing all the names found in the rows that include the name RicHARD.
    Also, since I can't quite figure out exactly what each line of your code is doing, will you please be gracious enough to put notes by each line explaining what it's doing?
    Again, thank you so much for your time and effort.
    Attachment 653830

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    It's what happens without a good enough explanation neither an expected result attachment …
    And without opening your attachment I did not wrote any answer !

    As my demonstration well works on my side for the result I imagine
    without using the button but adding first the formula in E20:H20 …

    A reminder for advanced filter criteria as written in its help :
    on a same row that means 'AND' the reason why logically it can't work
    except if each cell of a data row matches with the column criteria.
    For an 'OR' each added criteria must be alone on a below row …

    Your attachment is not valid ‼
    Just follow the top page yellow banner …

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    I forgot to point out your initial error :

    according to the advanced filter help, the criteria range must contains at least two lines,
    the first for column header, the second for column criteria, but in your code you forgot the header row #19 …

  6. #6
    Forum Contributor
    Join Date
    06-08-2012
    Location
    Panama City, FL
    MS-Off Ver
    Excel 2016
    Posts
    175

    Re: VBA macro to filter rows based on text entered in query cell

    Thanks Marc L. I followed your instructions concerning the horizontal AND and the vertical OR. I also corrected the error and included the header in the Criteria. It now works.
    I would greatly prefer if the same thing could be done without using the credential cells like your first script.

    Is that possible?

    I am including the final result below including an image of my scripts.

    Again, I appreciate your expert assistance.
    Attached Images Attached Images

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb

    Revamping your workbook in order to use only the NameSearch cell (if cleared all the data list is shown) :

    • deleting the FilterMacros module
    • so deleting the button ^^
    • deleting the results range

    • adding formulas in the criteria range
    • inserting a new column in first column for moving the criteria range and hidding it
    • namiing the criteria and the data list ranges

    • updating the event procedure in the worksheet module :

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        If 
    Not Intersect(Target(1), [NameSearch]) Is Nothing Then
            
    If IsEmpty([NameSearch]) Then
                
    If Me.FilterMode Then Me.ShowAllData
            
    Else
                [
    NameSearch].Select
                
    [NamesCol].AdvancedFilter xlFilterInPlace, [NamesCrit]
            
    End If
        
    End If
    End Sub 
    ► Do you like it ? ► ► So thanks to click on bottom left star icon « Add Reputation » !
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    06-08-2012
    Location
    Panama City, FL
    MS-Off Ver
    Excel 2016
    Posts
    175

    Re: VBA macro to filter rows based on text entered in query cell

    Marc L - I just want to say, your REVAMP is pretty incredible. I'm quite impressed and very appreciative.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    Thanks (for both the appreciation and the rep') but I just apply an Excel Basics like any beginner can - must ! - operate …

+ 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] Power Query / Get & Transform - filter text contains based on list
    By SHI.NL in forum Excel General
    Replies: 8
    Last Post: 05-14-2018, 04:53 PM
  2. filter data based on text entered or selected
    By lee2121 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-01-2016, 05:43 AM
  3. Macro to filter, sort, and then display top 20 rows based on value in particular cell
    By shaunguyver in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-03-2015, 07:18 AM
  4. Automatically Filter Data Based on what is entered into cell
    By bpyoung83 in forum Excel Programming / VBA / Macros
    Replies: 34
    Last Post: 09-07-2013, 03:54 AM
  5. [SOLVED] Macro - Filter Columns and Rows based on cell value
    By Fett2oo5 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2013, 04:26 PM
  6. Macro to Filter Table Based on Contents of a Cell which is Text
    By jgray in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-03-2013, 11:57 PM
  7. Filter Combo Box list items based on partially entered text
    By Balsams in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-12-2009, 12:57 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