+ Reply to Thread
Results 1 to 16 of 16

Make udf flexible to specific sheet

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Make udf flexible to specific sheet

    Hello everyone
    I have the following UDF that I need to fix to be flexible with any sheet not only active sheet
    It is searching for the last value in specific range

    Please Login or Register  to view this content.
    It is working well when I use it in the active sheet
    Please Login or Register  to view this content.
    I need to be able to use the UDF in another sheet
    Please Login or Register  to view this content.
    Thanks advanced for help
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Make udf flexible to specific sheet

    The unqualified use of the cells() method in the If statement means that VBA will use the active sheet. Qualify your use of the cells method to a specific range object. I assume you would want it to operate on the myrng object passed to the function.
    Please Login or Register  to view this content.
    In the long run, I find there is value in never (or almost never) using unqualified references to methods and properties -- especially in UDFs. I would suggest that you get in the habit of always explicitly specifying the object that a property or method applies to.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    Thanks for reply
    I tried using Parent to refer to the sheet but got VALUE error
    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Make udf flexible to specific sheet

    Parent is a property not an object: https://msdn.microsoft.com/en-us/lib.../ff196885.aspx

    myrng.parent...

    Is it necessary for the UDF to place the myrng object within its parent object (the sheet)? If I understand what you are doing, why not simply loop through the individual cells of the myrng object?

    Please Login or Register  to view this content.
    It probably has trouble if mrng can be a 2D range, but if myrng will always be a 1D range as your example shows, then this should work.
    Last edited by MrShorty; 09-28-2016 at 03:29 PM.

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    Thanks I tried your suggestion. It works but didn't find (not found) although the string is existing ...

    At first post it was working in the same sheet I was searching in and not working in another sheet
    Now it gives (Not Found) on both sheets

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Make udf flexible to specific sheet

    Adding the prefix as MrShorty suggested:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Make udf flexible to specific sheet

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    Thank you very much for these great solutions. Now it is working as expected
    Thanks a lot for help
    Best and Kind Regards

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Make udf flexible to specific sheet

    You're welcome.

  10. #10
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Make udf flexible to specific sheet

    shg, I think that he wanted a reverse lookup match rather than a forward lookup? He has a way to do both now.

    I normally add an Application.Volatile False for UDFs.
    e.g.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 09-28-2016 at 04:25 PM.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    but I tested shg udf and give me the last occurrence as expected

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Make udf flexible to specific sheet

    @Kenneth: I just had the same thought. OTOH, the other code only looks at the first column of the range. So perhaps

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    That's weird. At first it gives me right result but after testing it again , it gives me the first occurrence not the last
    Generally I used the UDF in post #10 and it is ok now

  14. #14
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Make udf flexible to specific sheet

    Obviously, #12 works fine too.

    Of course the Find method is more robust and usually the better and faster approach. You will have more options using that method and don't have to worry about the prefix issue. Of course the row number returned is subjective depending on the range used with the Find method and the default options or added options set.

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Make udf flexible to specific sheet

    Thank you very much Mr. shg for working and great solution. It is effective
    Thanks a lot Mr. Kenneth for sharing us. It is my pleasure to contribute in my thread
    Regards

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Make udf flexible to specific sheet

    You're welcome.

+ 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] Make UDF function flexible to any sheet
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-18-2015, 06:14 AM
  2. [SOLVED] How to make a flexible line chart in Excel
    By BNCOXUK in forum Excel General
    Replies: 3
    Last Post: 10-30-2012, 12:26 PM
  3. Replies: 4
    Last Post: 08-02-2012, 12:48 PM
  4. conditional sum: can I make the array flexible
    By jash147 in forum Excel General
    Replies: 4
    Last Post: 11-02-2007, 02:25 AM
  5. Replies: 1
    Last Post: 11-30-2005, 10:15 PM
  6. Can I make formulas more flexible?
    By George in forum Excel General
    Replies: 3
    Last Post: 11-08-2005, 01:40 PM
  7. [SOLVED] How to make a flexible cell reference?
    By Mar Vernooy in forum Excel General
    Replies: 2
    Last Post: 07-01-2005, 09:05 AM

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