+ Reply to Thread
Results 1 to 12 of 12

Boolean search - macro

  1. #1
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Boolean search - macro

    Hi Guys,

    I have a database with text in multiple cells. I want to have a search functionality to find certain words, for example to be able to find "orange" OR "yellow", or "orange" AND "yellow". What's more, I need excel to return results, of where these words are. So let's say it's my database:

    A1 A2
    Fruits This lemon is yellow.
    Books This book is orange.

    Now I'm putting that "orange OR yellow" and the excel is giving me: Fruits, Books.
    I'm putting "orange AND yellow" and the excel is returning 0 answers.

    Is there a chance to code something like that?

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

    Re: Boolean search - macro

    Definitely can be done but it depends on how your data is organized. Also, how do you want the use to enter these queries? Please provide a file with sample data and a samples of queries.

    BTW it would be better to use a real database like Access for this, because this kind of thing is built into SQL queries.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Re: Boolean search - macro

    Example workbook attached. Unfortunately I have to use Excel for this, not Access
    Attached Files Attached Files

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Boolean search - macro

    I get the idea of what you want to do but your sample data is just the same record over and over, so not useful for testing.

    If you want the user to be able to enter a free-form boolean expression like

    (orange OR yellow) AND blue

    then you have to have a grammatical parser and that is more work than I am able to do for you here.

    If you structure it so the use can enter some search terms and pick OR and AND is a little more manageable. An example of this is how Excel Autofilter works.

    and or.jpg

  5. #5
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Re: Boolean search - macro

    Thanks for this! I edited the sample workbook. Yes, the Excel Autofilter works the way I want, but as you mentioned, ideally if they also could put OR and AND in the same time
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Re: Boolean search - macro

    Does anyone have any idea?

  7. #7
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Boolean search - macro

    Does anyone have any idea?
    Try this:
    • The operators " AND " & " OR " must be capital letter, but without "(" and ")". For example:
      red OR blue AND book
      red AND blue AND glove OR ball
    • Type the keywords in C2 then hit commandbutton.
    • I'm assuming that data in sheet Database col A (Title) are unique.
    • The result (start at F6) may or may not be have the same order as data in col A (Title).


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

  8. #8
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Re: Boolean search - macro

    Works perfectly, thank you so much!!!

  9. #9
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Boolean search - macro

    You're welcome, glad to help & thanks for the feedback.
    Last edited by Akuini; 02-22-2022 at 08:26 AM.

  10. #10
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Boolean search - macro

    I forgot something, how big is your data?
    The code uses Application.Transpose which has a limit of 65536 item to process. So, if the result is more than 65536 items then it will generate a wrong result. Unfortunately when this happens there aren't any error message.
    So, if that could be that case then we need to amend the code.

  11. #11
    Registered User
    Join Date
    03-05-2021
    Location
    Poland
    MS-Off Ver
    365
    Posts
    36

    Re: Boolean search - macro

    Akuini, the data will be getting bigger with time but I don't think it will exceed 65536! Is that a number of rows?

  12. #12
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Boolean search - macro

    I don't think it will exceed 65536! Is that a number of rows?
    Yes.
    It's ok if data won't exceed 65K rows.

+ 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] Macro to substitute consonants for ? for Boolean search
    By hadydea in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-06-2020, 09:58 AM
  2. Boolean search - AND, NOT, and OR
    By nobleprince in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2019, 05:37 PM
  3. [SOLVED] Exit Workbook_BeforeClose(Cancel As Boolean) if a public boolean = true
    By meabrams in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2016, 06:10 AM
  4. macro for 14 boolean variables
    By flipper687 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-16-2014, 08:22 AM
  5. Help: Trying to boolean search down a column and note matches.
    By LightingPop in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-25-2012, 09:47 PM
  6. Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) problem
    By 2funny4words in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-29-2009, 07:28 PM
  7. Name Search in Excel User Sheet / Boolean?
    By Peter1999 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2007, 07:41 PM

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