+ Reply to Thread
Results 1 to 17 of 17

UDF referencing closed workbook not working

  1. #1
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    UDF referencing closed workbook not working

    Hi

    I built a udf that, in usage, references another workbook. Basically, it has the user provide a range, and then searches that range for various text. It uses a For each object in sheet.cells loop. I've found the formula only works when the workbook it is referencing is open. Is that correct? What is the reason?
    Last edited by davegugg; 05-26-2010 at 09:30 AM.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  2. #2
    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: UDF referencing closed workbook not working

    How else could the UDF go through the contents of the workbook if it weren't open?

    You can retrieve single values using ADO, but using the common elements of the object model applies to open workbooks.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    Why is it that excel's native functions work? Do they use a different process to get the values? For example vlookup?

  4. #4
    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: UDF referencing closed workbook not working

    Some Excel functions work -- the ones that work on closed workbooks. INDIRECT, for example, doesn't.

  5. #5
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    Out of curiosity, do you know of a rhyme or reason for which work and which don't? I don't see any difference between the way vlookup looks at a spreadsheet in a workbook and the way Indirect does the same.

  6. #6
    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: UDF referencing closed workbook not working

    I believe Harlan Grove explained it this way:
    Functions that either return range references or require particular arguments to be range references don’t work with closed workbooks:
    OFFSET 1st argument must be a range reference, and it returns a range reference. Excel evaluates external references into closed workbooks as either scalar values or arrays, not as range references in Excel's strict working definition of 'range'.
    INDIRECT Always returns a range reference.
    RANK 2nd argument must be a range reference, no matter what online help says.
    COUNTIF 1st argument must be a range reference.
    SUMIF 1st and optional 3rd arguments must be range references.
    CELL Optional 2nd argument must be a range reference.

  7. #7
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    Ok, when I test out Grove's list, I can see the arguments he refers to are called: reference, ref_text, ref, and range. However, vlookup's argument that I would expect to be the same is called table_array. After selecting a range for CountIf and Vlookup, the range looks exactly the same in the formula bar. So I guess I'm not seeing the difference.

    That being said, could I somehow use however Vlookup's "table_array" argument works with my UDF defined below?

    Please Login or Register  to view this content.
    This is a generic udf, I know there are built-in excel alternatives. I'm really just curious about the differences I'm seeing.

    Thanks shg

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: UDF referencing closed workbook not working

    The difference is that VLOOKUP will accept an array as the lookup table, rather than a range. COUNTIF has to have a range.
    Remember what the dormouse said
    Feed your head

  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: UDF referencing closed workbook not working

    The difference is, for VLOOKUP and MATCH and others, the second argument is commonly a range, but CAN BE a literal array:

    =MATCH(3, {1,2,3,4,5}, 0) works fine.

    In VBA, it can also be an array:

    Please Login or Register  to view this content.
    This doesn't work, because the first argument MUST BE a range:

    =COUNTIF({1,2,3,3,4}, 3)

    Ditto for

    Please Login or Register  to view this content.
    If you're using WorksheetFunction, IntelliSense will tell you which arguments must be ranges.

  10. #10
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    Sorry if I'm being obtuse, but either way doesn't excel have to go in and look at the values of a spreadsheet in a closed workbook? In my udf above could I ask for an array instead of a range and thus be able to use it on a closed workbook?

  11. #11
    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: UDF referencing closed workbook not working

    VBA and worksheet functions are not the same thing. In VBA, a range is a collection of cells in an open workbook. If you attempt to pass a range in a closed workbook to a UDF, you'll never arrive at the UDF.

  12. #12
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    So would it be correct to say excel worksheet functions have some special functionality that can't be accomplished by using vba?

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: UDF referencing closed workbook not working

    That's not the issue - your UDF requires a range, not an array of values, which is why it doesn't work (just like COUNTIF). If your UDF worked with an array of values, you would be fine.

  14. #14
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    I get what you are saying about the range, my udf requires a range, not an array. My question now is why when I type a valid vlookup formula into a sheet it can find the correct value even if the workbook it is referencing is closed. How does it get the array it is working on out of the closed workbook?

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: UDF referencing closed workbook not working

    Because VLOOKUP can work with an array of values, which is what it retrieves from the closed workbook. COUNTIF (for example) requires an actual range, which it cannot retrieve from a closed workbook, so does not work with closed workbooks.

  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: UDF referencing closed workbook not working

    The answer to that is buried in the bowels of Excel, but what difference does it make, Dave?

    You can pass an array of values from a closed workbook to a UDF, you cannot pass a range.

    This works:

    =MyFunc('C:\some Path\[some closed Workbook.xls]some Sheet'!A1:A10)

    ... if MyFunc is like this

    Please Login or Register  to view this content.
    ... and not like this:

    Please Login or Register  to view this content.
    EDIT: Consider that a Range has many more properties than Value; it has cell formatting, text formatting, rich-text formatting within cells, comments, dependents, formulas, height & width, ...
    Last edited by shg; 05-25-2010 at 06:29 PM.

  17. #17
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: UDF referencing closed workbook not working

    Ok, I think it's finally sinking in. That last post was helpful shg, thank you.
    Thanks romperstomper. I appreciate the instruction.

+ 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