+ Reply to Thread
Results 1 to 6 of 6

VBA Find Lookat and Lookin as variables

  1. #1
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    VBA Find Lookat and Lookin as variables

    Hi all

    Im having a little trouble with the Lookin and Lookat parameters in the Find function, as I want to get the xlValues/xlFormulas and xlWhole/xlPart options from a cell value, like below:
    SearchVariants.png

    Im pretty sure the problem is due to the variable types, but not sure how to fix it.

    So this simple macro works:
    Please Login or Register  to view this content.
    But this does not:
    Please Login or Register  to view this content.
    I can work around it, by adding this If else section, where I convert for example "xlValues" to xlValues, but how can I avoid that?:
    Please Login or Register  to view this content.
    Best regards
    Imbizile
    Attached Files Attached Files

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,283

    Re: VBA Find Lookat and Lookin as variables

    The alternative is to store the literal values of the xlFormulas and xlValues constants (-4123 and -4163 respectively) and use those instead of the text.
    Remember what the dormouse said
    Feed your head

  3. #3
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: VBA Find Lookat and Lookin as variables

    interesting! first time I've ever heard this question asked before, on any board. and I have accounts on 20 of them. =) here is your answer, Imbizile. and thanks for asking! very intuitive.

    this code of yours:
    Please Login or Register  to view this content.
    works because you have put what are called enumerations into function arguments. the compiler (the machine) reads those as 2 bit pieces of information, generally. But they can take other values depending on how any given program is written. see this for a relevant example of an enumeration that is important to you regarding this topic:

    https://docs.microsoft.com/en-us/off...l.xlfindlookin

    most functions and methods in every language take enums as arguments. especially those that are not complex inputs. complex inputs tend to be required in the form of variables because the operation conducted ON them has to be complex. the most basic example of an enum, although it is technically not one, is the boolean data type. In VBA, the values for true and false are 0 and -1. or, sometimes it may even be 0 and 1. Not sure.

    so, an example as to why you're not getting certain things to work, this code of yours:
    Please Login or Register  to view this content.
    works fine because LookInMode is a variant, and thus can hold any data type. xlValues is not a string. It is a number. specifically, it is -4163. the humans read alpha characters, not numbers. that's why Microsoft tells you to write English instead of numbers. the compiler takes care of the rest. so in the above code example, you are actually telling the compiler this:
    Please Login or Register  to view this content.
    and MS says that is acceptable: https://docs.microsoft.com/en-us/off...l.xlfindlookin. But, coincidentally, many other platforms that are much better in the way they are written would never see this sort of behavior by the developer and/or the environment as acceptable.

    so the answer to your question is: no, you can't workaround it. but you can get what you want by other means. perhaps putting the actual numbers in the sheet, in a hidden way so people don't see them? then putting the actual words you want to see in the same place in the visual sense? then have your code read the hidden numbers. that makes good sense.
    Last edited by vba_php; 11-24-2020 at 01:31 PM.

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: VBA Find Lookat and Lookin as variables

    You Omitted xlComments in your the Data Validation for D3. That would look for "Test" in a note.

    Please Login or Register  to view this content.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  5. #5
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: VBA Find Lookat and Lookin as variables

    Thanks for the suggestions everyone and especially thanks for the very detailed explanation for this behaviour, Adam.

    Not sure which solution I will go for yet though.

    @mehmetcik, I omitted xlComments on purpose, as I only need to search in actual values, but cheers for pointing that out

  6. #6
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: VBA Find Lookat and Lookin as variables

    Quote Originally Posted by Imbizile View Post
    especially thanks for the very detailed explanation for this behaviour, Adam.
    yeah well I always go overboard with explaining things. thanks for not complaining about it. good deal. glad it made an impact. that's obviously always the goal. =)

+ 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] Using LookAt:=xlWhole constant with Find-Not working
    By Pawnar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-22-2020, 04:11 AM
  2. [SOLVED] vba excel - Find LookAt not matching the inputted value
    By terriertrip in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-08-2018, 01:27 PM
  3. [SOLVED] Find Method - can't get LookAt:=xlWhole to work
    By mo4391 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-08-2016, 09:04 AM
  4. Macro - Find and Replace - LookAt Issues
    By drewmey in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 10-07-2014, 04:42 AM
  5. [SOLVED] lookat whole
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2012, 06:10 AM
  6. Find and replace is missing Value lookin...
    By highwyre237 in forum Excel General
    Replies: 1
    Last Post: 09-04-2009, 12:29 PM
  7. Replies: 4
    Last Post: 11-10-2005, 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