+ Reply to Thread
Results 1 to 6 of 6

Find Method returns a string instead of a Range

  1. #1
    Registered User
    Join Date
    06-03-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    59

    Find Method returns a string instead of a Range

    For some reason, it seems that this following line returns the value its supposed to be searching for instead of the range where it finds the value.

    Please Login or Register  to view this content.
    Any ideas on why this might be the case?

    Thanks
    Last edited by Kaigi; 06-24-2009 at 04:49 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find Method returns a string instead of a Range

    Hello Kaigai,

    You're probably referring to the tool tip in VBA that shows the result of the find when you place the mouse on the variable. It will display the cell contents. To return the address, do this...
    Please Login or Register  to view this content.
    Also, you should get into the habit of declaring your variables. This can save you major headaches in coding.

    However, if the search fails then dir_ee will be set to the special variable Nothing.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    06-03-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    59

    Re: Find Method returns a string instead of a Range

    Thanks for the tip about Find... didn't know it would display the cell content instead of the address even though its an address value.

    It turns out the Find was working fine, but the problem was that I neglected to specify which worksheet to get the data from on one of the following lines of code that referenced the found result.

    Actually I have a question about variables. I declare all of my variables except for the ones that I use in "Set ...". Should I be declaring those ones too? Why do we sometimes say "Set ... " and other times use <variable> = <value>

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find Method returns a string instead of a Range

    Hello Kaigi,

    Set is used to assign an Object variable to either an existing object or a new object. If you know the object type your object variable will be assigned to, you should declare it as the assigned type.

    For example, in your code the variable dir_ee is being assigned to a Range. You can then declare dir_ee as a Range like this...
    Please Login or Register  to view this content.
    Sometimes you don't know the type of object returned. In this case assign your object variable as Object. This is a generic form that accepts all types of objects. The drawback is you don't get the IntelliSense feature which shows the object's properties, methods, and events as you type in VBA.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-03-2009
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    59

    Re: Find Method returns a string instead of a Range

    Ah ok - I'll declare those from now on too then.

    Thanks for all the clarification and help.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Find Method returns a string instead of a Range

    Hello Kaigi,

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ 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