Consulting

Results 1 to 8 of 8

Thread: Solved: Referencing UDF from another workbook; Evaluate?

  1. #1
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location

    Solved: Referencing UDF from another workbook; Evaluate?

    Hi guys ,

    Not sure if this is feasible. I have an XL file containing a custom UDF. This particular file is open concurrently with another workbook. Would there be some way to make the UDF available to any other workbook? I only need to use this UDF on rare occasions so I guess, rather copying the UDF in question to the new workbook to work on, I want to do a short cut here.

    Thanks in advance for your thoughts on this.

    Regards



    kp

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]=Book2.xls!test(A1,B1)[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Hi MD,

    Thanks for your quick response. Sorry I don't quite get it.
    Adopting the suggested solution I get a popup file open window. This is an example. Say, I have the following UDF in a file:

    [VBA]Function ShCount()
    ShCount = ActiveWorkbook.Sheets.Count
    End Function[/VBA]

    How do I make reference to this UDF in a code from another workbook which is also active? For instance:

    [VBA]
    If ShCount > 15 then .....[/VBA]

    Regards


    kp

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is how you call a sub. I've never tried to call a function.
    [vba]
    c = Application.Run("book2!test", Range("A1"), Range("B1"))

    [/vba]

    My previous post was intended to be entered on a spreadsheet.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA]
    'In Book 1
    Sub Macro1()
    c = Evaluate("=Book2.xls!test(B11, D11)")
    End Sub
    'In Book 2
    Function Test(ff1, ff2)
    Test = ff1 * ff2
    End Function
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Brilliant. Thanks MD. Forgot about the old Evaluate - a very handy function indeed. Really appreciate your help MD.

    Have a great day.

    Regards


    kp

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Glad it worked out!
    Please mark it solved using the Thread Tools dropdown.
    Regards
    MD
    PS
    I've amended your title just to help any future searches.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Contributor
    Joined
    Apr 2006
    Posts
    144
    Location
    Thanks again MD. Thread now noted as solved.

    Cheers


    kp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •