+ Reply to Thread
Results 1 to 16 of 16

Code for multiple search

  1. #1
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Code for multiple search

    Hello, I want to write a code that allows me to look in multiple columns for specified entries.
    For example, I want to search column A for the word "John", if found, I want to check column B for hours 1:00, if found I want to check column C for hours 1:00, finally if found, I want to check column D for the letter "C".
    And in the end, return the number of the row so I can use it in my code.
    In the sample below, the code should return the number 9.
    I have a lot of entries in a sheet1, I am trying to build a code, that will look in the sheet1 if specific entries (which I provide in sheet2) exist.
    Is this possible?
    Any help will be appreciated. Thanks
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    Here is a sumproduct formula that will find the row (assuming that there is only 1 unique result to be found)

    =SUMPRODUCT(--(A2:A9="John"),--(B2:B9=TIMEVALUE("01:00")),--(C2:C9=TIMEVALUE("01:00")),--(D2:D9="C"),ROW(D2:D9))

    You could either use this directly on the sheet to get your result, or use the EVALUATE function to have it work directly within your script.

    rylo

  3. #3
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    Thanks rylo, how do I write it in vba code?

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    HI

    one way

    Please Login or Register  to view this content.
    rylo

  5. #5
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    Thanks for your help, and sorry for the late reply.
    I tried using the code you wrote, but I think there is something wrong with what I am writing, because I am getting an error.
    this is my code:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    By the way, this code is not related to the sample I posted.

  7. #7
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    Only looked at the one line but try something like

    Please Login or Register  to view this content.
    rylo

  8. #8
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    It is not working. I still get
    run time error 13
    type mismatch

  9. #9
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    OK, as it isn't related to the example file, can you put up a file that it relates to, with the amended code and I'll have a look.

    Also you don't seem to have destrow2 filled anywhere and
    Please Login or Register  to view this content.

    rylo
    Last edited by rylo; 01-22-2013 at 04:18 AM.

  10. #10
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    ok here is a sample.
    You see I am making a logbook, where I enter flight details in the AllData sheet, and when I select "ok" in column L, the code should automatically copy the information to the pilots sheets and make some adjustments.
    The code I asked before should work with this one when I select "CLR".
    For example, if I select CLR in AllData sheet in row 6, the code should go to the sheet (pilot3) and delete the row that has the same information as this one. (in this case "row 5" in "pilot3" should be deleted).
    And also, it should do the same for the co-pilot (in this case "row 5" in sheet "pilot1" should be deleted).
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    It is related to the way you have your if / end if blocks positioned.

    What I did was actually indent your code logically and from this I found that you have
    Please Login or Register  to view this content.
    but you don't have an else for that test. All the following if / end if and with statements are all related to that block, so it is not doing any testing for the 'CLR' option when you do the selection.

    I've put in an else statement that will cover of the selection of the 'CLR' option to just show a messagebox of the target.value

    Please Login or Register  to view this content.
    See if that gets you going.

    rylo

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    Another way to do this would be to just delete the row in alldata for a 'CLR', then have worksheet activation events for each of the sheets so that they are updated with relevant data when they are activated.

    This is for the pilot1 sheet, missing the logic for the PIC/SIC.

    Please Login or Register  to view this content.
    rylo

  13. #13
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    Sorry rylo, that's not what I want...
    the code should look something like this:
    Please Login or Register  to view this content.
    but there is something wrong, I just can't figure it out
    Last edited by chiidzzz; 01-23-2013 at 04:30 PM.

  14. #14
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    The code works fine and does the job if I select "ok"
    The problem is with the last part, If I select "CLR"
    And when I click debug, the error seems in the (destRow3 =Evaluate...) part
    Last edited by chiidzzz; 01-23-2013 at 03:39 PM.

  15. #15
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Code for multiple search

    Hi

    Just because you have put in the with statement to action myDestSheet3, the formula being evaluated won't refer to that sheet. So the evaluate / sumproduct approach will have to include the relevant sheet name

    Please Login or Register  to view this content.
    The above does at least not error, but on the quick test I did, it didn't bring back a meaningful row. I'll leave that to you to investigate.

    rylo

  16. #16
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Code for multiple search

    Yes it does no error, but its value is always 0.
    I don't know how to continue...thanks for your help anyway

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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