Results 1 to 3 of 3

solving #Value error on User defined formula reading from different workbooks

Threaded View

  1. #1
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Arrow solving #Value error on User defined formula reading from different workbooks

    Hi, this is the situation:
    I have a macro that creates a number of rows and in some columns there are formulas that call userdefined formulas and external files to get results. When I execute the macro to create the rows there is no problem and even work with solver.

    if at some point one of the external files is closed and a formula is edited (with no actual change) the formula returns "#Value!"
    to sove that I can reopen the file and by going to edit-->replace "=" by "=" all gets recalculated and correct results are returned. (normal recalculate --F9-- does not work, but I can live with that)

    up until here is quite understandable.

    HOWEVER:

    I recorded a macro to do it exactly programatically and it does not work
    any clues on the reason / possible workaround??


    Sub Macro13()
        
    '
        Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub

    and this is the user defined function:

    Public Function InterpolateFromExperimentalData(ByVal Q As Variant, ByVal t As Variant, fn As String) As Double
        'Q is the horizontal dimension
        'T is the vertical dimension
        Dim owb As Workbook
        Dim f As Worksheet
        Dim lc As Integer
        Dim lr As Integer
        
        
        For Each wb In Application.Workbooks
            If wb.fullname = fn Then Set f = wb.Worksheets("DATA")
        Next
        
        If f Is Nothing Then
            Set owb = Workbooks.Open(fn, readOnly:=True)
            Set f = owb.Worksheets("DATA")
        End If
        
        lc = lastColumn(ws:=f, r:=1)
        lr = LastRow(ws:=f, C:=1)
        
        
        InterpolateFromExperimentalData = Linearinter22d(f.Rows(rgr(1, lr)).Columns(rgc(1, lc)), t, Q)
        
        
    End Function
    and other formulas contained

    Public Function Linearinter22d(inputs As Range, x As Variant, y As Variant) As Double
    'adapted from http://www.ozgrid.com/forum/showthread.php?t=32957&p=165188#post165188
    
        Dim nx As Long, ny As Long
        Dim lowerx As Long
        Dim lowery As Long
        Dim upperx As Long
        Dim uppery As Long
        Dim i As Long
        
        nx = inputs.Rows.Count
        ny = inputs.Columns.Count
        
        If x <= inputs.Cells(2, 1) Then
            lowerx = 2
            upperx = 3
        ElseIf x >= inputs.Cells(nx, 1) Then
            lowerx = nx - 1
            upperx = nx
        Else
            For i = 2 To nx
                If inputs.Cells(i, 1) >= x Then
                    upperx = i
                    lowerx = i - 1
                    Exit For
                End If
            Next
        End If
        
        If y <= inputs.Cells(1, 2) Then
            lowery = 2
            uppery = 3
        ElseIf y >= inputs.Cells(1, ny) Then
            lowery = ny
            uppery = ny - 1
        Else
            For i = 2 To ny
                If inputs.Cells(1, i) >= y Then
                    uppery = i
                    lowery = i - 1
                    Exit For
                End If
            Next
        End If
        
        Dim XL As Double, XU As Double, YL As Double, YU As Double
        Dim temp1 As Double, temp2 As Double
        
        XL = inputs.Cells(lowerx, 1)
        XU = inputs.Cells(upperx, 1)
        YL = inputs.Cells(1, lowery)
        YU = inputs.Cells(1, uppery)
        
        temp1 = (inputs.Cells(lowerx, lowery) * (XU - x) _
            + inputs.Cells(upperx, lowery) * (x - XL)) / (XU - XL)
        
        temp2 = (inputs.Cells(lowerx, uppery) * (XU - x) _
            + inputs.Cells(upperx, uppery) * (x - XL)) / (XU - XL)
        
        Linearinter22d = (temp1 * (YU - y) + temp2 * (y - YL)) / (YU - YL)
    
    End Function
    
    Function rgr(ByVal firstRow As Integer, ByVal LastRow As Integer) As String
        'returns the string to refer to a range of rows as in ws.rows(string)
        rgr = firstRow & ":" & LastRow
    End Function
    
    Function rgc(ByVal firstcolumn As Integer, ByVal lastColumn As Integer) As String
        'returns the string to refer to a range of columns as in ws.columns(string)
        rgc = N2L(firstcolumn) & ":" & N2L(lastColumn)
    End Function
    
    Public Function N2L(num As Integer) As String
        'returns letter corresponding to column number
        N2L = Split(Cells(1, num).Address, "$")(1)
    End Function
    Last edited by bagullo; 05-25-2012 at 09:38 AM. Reason: clarity

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.6.0 RC 1