+ Reply to Thread
Results 1 to 14 of 14

Thread: UDF causes #value error whenever copying cells

  1. #1
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Smile UDF causes #value error whenever copying cells

    1) The following code works well except for 1 wierd circumstance. This UDF code resides in a Cell in workbook 'A'. If I have another workbook open (call it workbook 'B'), and I do ANYTHING in workbook 'B' (like copy a cell to another cell within workbook 'B', or perform a calculation within workbook 'B', or even copy a cell from 'A' to 'B'), then the cell that this UDF resides in (workboook 'A') will change from a good number to a #VALUE error. If I work ONLY within 'A', then this UDF functions properly (giving a good result).
    2) If I hit the F9 button (i.e. re-calc), with workbook 'A' active, then the #VALUE goes away, and the good number comes back. To clarify, all workbooks have AUTO CALC enabled. I guess the 'Application.Volatile' line causes this code to run when I hit the F9 button (thereby clearing the #VALUE error). If I hit F9 with 'B' active, then the #VALUE error (back in 'A') goes NOT clear.
    3) NOTE #1: If I put a BREAKPOINT inside this code, and then repeat a "copy from cell to cell" within workbook 'B', I find that the following code DOES NOT execute, as it does NOT "stop" the code at the BREAKPOINT line. But, when I hit F9, then it DOES stop at the BREAKPOINT.
    4) NOTE #2: If I "comment out" the Application.Volatile line, it still does the SAME thing. Except worse, then when I hit F9, it doesn't correct the #VALUE error. I really need the Application.Volatile step with this code, as these functions will not automatically update if their referenced cells are changed.
    5) NOTE #3: The variables Row_Num & Column_Num are declared at the Module Level. If I use unique variables and declare these at the Procedure Level, it makes NO difference. I also tried putting a "DoEvents" line after Application.Volatile, but NO difference.
    6) It seems I need this code to execute whenever there is a "change" on ANY workbook. Or else, do something to keep these cell values from changing anytime there is "action" on another workbook. Or, is there better code to do the samething? ... Do you have any CLUE how to fix this SNAG???

    Function GetQLT(Search_Item As Variant, Header_Code As Variant) As Variant
     Application.Volatile True 'Forces function to re-calc if anything changes on worksheet
     Row_Num = WorksheetFunction.Match(Search_Item, Range("DOC.QltTbl.PrdFam.c"), 0)
     Column_Num = WorksheetFunction.Match(Header_Code, Range("DOC.QltTbl.IT_Name.r"), 0)
     GetQLT = WorksheetFunction.Index(Range("DOC.QltTbl.x"), Row_Num, Column_Num)
    End Function
    Last edited by sauerj; 01-27-2010 at 10:03 PM. Reason: Problem Solved by Sng and DonkeyOte

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: UDF causes #value error whenever copying cells

    I'm not sure it's worth getting into the debate as to why you're using a UDF to do this but...

    Given the UDF has been made Volatile cells utilising it will recalculate upon any Volatile action taking place.

    It follows that if that action occurs in another file then the unqualified range references will become invalid given they are applied to the Active Worbook which is not the one containing the relevant ranges.... so perhaps utilise Application.Caller ?

    (there may be better methods though... generally speaking I try to avoid Volatile UDFs so don't have a great deal of experience)
    Last edited by DonkeyOte; 01-26-2010 at 12:44 PM. Reason: worth not whether... the later it gets the less sense I make...

  3. #3
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: UDF causes #value error whenever copying cells

    If you pass the ranges you need, the function will recalculate automatically without requiring it to be volatile:
    Function GetQLT(vRowFind As Variant, vColFind As Variant, _
                    rRowLook As Range, rColLook As Range, _
                    rTbl As Range) As Variant
        
        Dim iRow        As Long
        Dim iCol        As Long
    
        With WorksheetFunction
            iRow = .Match(vRowFind, rRowLook, 0)
            iCol = .Match(vColFind, rColLook, 0)
            GetQLT = rTbl(iRow, iCol).Value
        End With
    End Function
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    641

    Re: UDF causes #value error whenever copying cells

    I haven't ben able to reproduce your problem with the code that you provided. It appears to work just fine. Maybe you have narrowed down your problem into the work area. I would check your definitions of the range variables. Are they static, or are you changing them?
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: UDF causes #value error whenever copying cells

    The problem is as DO described, and it could be solved with Application.Caller, but it's bad practice not to pass UDFs all of their dependents and then try to paper it over by making it volatile.

    EDIT:
    Quote Originally Posted by DO
    ... why you're using a UDF to do this but ...
    That too.
    Last edited by shg; 01-26-2010 at 12:48 PM.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: UDF causes #value error whenever copying cells

    Thanks shg - yes I should perhaps have elaborated on that myself - I'm not condoning Volatile UDFs ... but I'd say if you end up passing all range variables you may as well just use the native syntax.

    This is an interesting one though... a while ago my old boss used to complain how long it took to type in an INDEX with embedded MATCH functions etc... so I spent some time coming up with an automatic shorthand translator - don't ask...
    ie you type in short hand and it wrote back the native function... I know... I'll see if I can find it (so you can have a laugh at my expense)
    Last edited by DonkeyOte; 01-26-2010 at 12:56 PM.

  7. #7
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: UDF causes #value error whenever copying cells

    Ha, I found it....

    Private Sub Worksheet_Change(ByVal target As Range)
    Dim str_temp As String, str_formula As String
    Dim v_args As Variant
    Dim cell As Range, rng_tbl As Range
    Dim l_rng_tbl_rows As Long, l_rng_tbl_cols As Long
    On Error GoTo Fatal
    For Each cell In target.Cells
        If InStr(cell.Formula, "=TLOOKUP") = 0 Then Exit Sub
        str_temp = Replace(cell.Formula, "=TLOOKUP(", "")
        str_temp = Left(str_temp, Len(str_temp) - 1)
        v_args = Split(str_temp, ",")
        'define INDEX left most cell
        Set rng_tbl = Range(v_args(2))
        'define INDEX table based off left most cell and rows/columns
        l_rng_tbl_rows = Cells(rng_tbl.Parent.Rows.Count, rng_tbl.Column).End(xlUp).Row - (rng_tbl.Row - 1)
        l_rng_tbl_cols = Cells(rng_tbl.Row, rng_tbl.Parent.Columns.Count).End(xlToLeft).Column - (rng_tbl.Column - 1)
        Set rng_tbl = rng_tbl.Resize(l_rng_tbl_rows, l_rng_tbl_cols)
        'test to see if Target intersects range - if so means error -- ie table to left of TLOOKUP formula (circular references)
        If Not Intersect(target, rng_tbl) Is Nothing Then GoTo Fatal
        'translate formula to non-volatile equivalent thereof
        str_formula = "INDEX(" & rng_tbl.Address & "," & _
                            "MATCH(" & v_args(0) & ",INDEX(" & rng_tbl.Address & ",0,1),0)," & _
                            "MATCH(" & v_args(1) & ",INDEX(" & rng_tbl.Address & ",1,0),0)" & _
                        ")"
        If UBound(v_args) = 3 Then str_formula = "IFERROR(" & str_formula & "," & v_args(3) & ")"
        're-enter revised formula (disable events)
        Application.EnableEvents = False
        cell.Formula = "=" & str_formula
        Application.EnableEvents = True
        'release memory
        Set rng_tbl = Nothing
    Next cell
    Exit Sub
    
    'handler to enforce events
    Fatal:
    MsgBox "Er... Error..."
    Application.EnableEvents = True
    End Sub
    Pre-emptive defences...

    a) I wrote this a while ago (in an afternoon as I recall)
    b) In the above guise it's aimed at 2007 (use of IFERROR)
    c) it assumes match_type of 0 at all times
    d) it expects to be entered stand alone, ie not embedded or used multiple timeswithin one function

    Basic premise being that to enter the INDEX/MATCH you would simply enter:

    =TLOOKUP(horizontal criteria,vertical criteria,top left cell of table [,optional error value])
    on entry the above would convert to native function syntax so as to keep non-UDF but allow for short hand entry.

    seemed a good idea (for about a day)

    Might be useful to someone... you never know.
    Last edited by DonkeyOte; 01-26-2010 at 12:59 PM.

  8. #8
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: UDF causes #value error whenever copying cells

    I like this line best:
        MsgBox "Er... Error..."
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: UDF causes #value error whenever copying cells

    Ha, these days I prefer to use "Computer Says No" as my dialog...

    This is based on a classic sketch from the comedy show over here "Little Britain"

  10. #10
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Re: UDF causes #value error whenever copying cells

    * Thanks DonkeyOte and Sng. Interesting how Ranges become "unqualified" when working in other workbooks (never would have thought).
    * Sng's tip on including the Ranges in the UDF arguments fixed the problem, but I am trying to get the Application.Caller idea to work. ... Why?
    * I want the cell formula to be as TIGHT as possible. This UDF is inside of an already HUGE cell formula with a large 'IF' and lots of other cell math. My impatient co-workers would NEVER have the patience to deal with additional MATCH and INDEX functions on top of what these cells already contain ... nor would they bother to understand how these functions work so it wouldn't likely be used over the course of time causing future problems. ... I'll mark this one SOLVED and tip your scales. ... It amazes me how fast and helpful this forum is. ... 3 for 3 home runs so far. Love it! ... Thanks again.

  11. #11
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: UDF causes #value error whenever copying cells

    Quote Originally Posted by sauerj
    I am trying to get the Application.Caller idea to work
    In the most basic terms

    With Application.Caller
        Row_Num = WorksheetFunction.Match(Search_Item, .Range("DOC.QltTbl.PrdFam.c"), 0)
        'etc...
    End With
    You say your co-workers are impatient ... well UDF's are generally speaking slower than native functions

    It might be worth having a read though of Charles Williams' thoughts on the matter: http://www.decisionmodels.com/calcsecretsj.htm

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

    Re: UDF causes #value error whenever copying cells

    ..., well UDF's are generally speaking slower than native functions
    Not to mention having them recalculate unnecessarily.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  13. #13
    Forum Contributor sauerj's Avatar
    Join Date
    11-11-2009
    Location
    Lafayette, IN
    MS-Off Ver
    Excel 2003
    Posts
    116

    Re: UDF causes #value error whenever copying cells

    * DonkeyOte ... Thanks for 2nd tip!!!
    * The co-workers add to and modify the cells in this worksheet. The impatience I refer to is their unwillingness to understand how to work w/ these formulas. They don't even know what "Range Names" are let alone learning how to work with INDEX and MATCH functions. (I don't pretend to be an Excel guru either.)
    * I'm doing a whole generation upgrade on this large budget accounting worksheet as it has become a complete MESS over the years due to its many additions (rendering it very ineffective for the group ... certainly not what it is intended to do).
    * THANKS AGAIN; you guys are VERY HELPFUL!

  14. #14
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: UDF causes #value error whenever copying cells

    FWIW, I would put good money on the fact that your worksheet needs a major redesign, if that's part of an already huge formula...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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.2.0