0

I've been trying the solutions I've found for similar questions, but it doesn't seem to be working to resolve my problem.

I have an Access database (Access 2016), which opens an Excel spreadsheet (Excel 2016) which does calculations. I also have an Excel Add-In file that contains a ton of UDFs. By default, I always have this Add-In installed in Excel and it's part of my profile boot script. However, when Access opens Excel through code, it doesn't recognize the Add-In (whereas opening Excel myself does work). So I've added code (per other forum suggestions) to uninstall and reinstall the Add-In after Excel opens. See code below.


    Dim appExcel As Object
    Dim strpath As String
strpath = "H:\Folder\Calculator.xlsm"
Set appExcel = CreateObject("Excel.Application") appExcel.Workbooks.Open FileName:=strpath, UpdateLinks:=1, ReadOnly:=True
Set wbk = appExcel.ActiveWorkbook appExcel.AddIns.Add FileName:="H:\Folder\AddInFile.xlam"
If appExcel.AddIns("AddInFile").Installed = False Then appExcel.AddIns("AddInFile").Installed = True Else appExcel.AddIns("AddInFile").Installed = False appExcel.AddIns("AddInFile").Installed = True End If
Set appExcel = Nothing
The only way to get my UDFs to work correctly requires me to go into every cell that contains one of these functions and manually force it to recalculate (F2-Enter). Forcing a recalculation on the entire worksheet doesn't help. It must be cell by cell. The formulas "execute" but have incorrect results until I force them to recalculate. Below is an example of one such function.

    Function Nx(Age, table, Interest)
Dim l(0 To 121) As Double Dim D1(0 To 121) As Double Dim N1(0 To 121) As Double Dim v As Double Dim NAge As Double
If Age > 120 Then NAge = 120 Else NAge = Age End If
l(0) = 9999.9999 v = 1 / (1 + Interest) D1(0) = l(0) Nn = D1(0) x1 = Int(NAge) x2 = Int(NAge) + 1 Fract = NAge - x1
For j = 1 To 120 l(j) = l(j - 1) * (1 - q(table, j - 1)) D1(j) = l(j) * (v ^ j) Nn = Nn + D1(j) Next j
N1(0) = Nn
For j = 1 To 120 N1(j) = N1(j - 1) - D1(j - 1) Next j
Na = N1(x1) - D1(x1) * (11 / 24) Nb = N1(x2) - D1(x2) * (11 / 24) Nx = ((1 - Fract) * Na) + (Fract * Nb)
End Function
This function works perfectly under any other circumstance. However, when you open Excel programmatically, it stops returning correct results. If you save the file and reopen it, everything updates correctly.

I can't seem to figure out a solution. I previously asked this question, but the first response requested a sample of code and down-voted my question, and after a few weeks no one had tried answering me. I really need to figure out a solution to this, so I figured I would try again with a fresh question.

12
  • 2
    Try to make it volatile. Use Application.Volatile in your UDF as first line. Commented Jun 29, 2018 at 13:29
  • Try loading the add-in before you open the desired workbook. You'll probably need to load a new blank workbook before you can add the add-in. Commented Jun 29, 2018 at 13:35
  • If you open it programmatically, use application.calculatefull on the excel.application object. Commented Jun 29, 2018 at 13:35
  • 1
    When Excel is opened via automation, add-ins are not loaded. You will need to load them via your code. Commented Jun 29, 2018 at 16:19
  • BTW you don't need to uninstall and reinstall the addin:just open it as if it was an ordinary workbook in your access code that starts Excel. Commented Jun 29, 2018 at 16:45

1 Answer 1

0

Put,

application.volatile 

as the first line of your function's code. Alternately or in concert, use,

application.calculatefull 

on the excel.application object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.