如何在VBA参考中永久打开SOLVER?

时间:2019-08-30 06:36:49

标签: excel vba

REFERENCE中的SOLVER必须激活才能在VBA中启用SOLVEROK和SOLVERSOLVE,但是每次我退出Excel时,VBA始终会自动关闭SOLVER。我该如何强制而不是暂时但不断地激活SOLVER?谢谢。

1 个答案:

答案 0 :(得分:1)

应该可以,但文件目录取决于您的Office版本(在我的情况下为Office15)

Sub addsolver()
    Dim i As Long, x As Long

    x = ThisWorkbook.VBProject.References.Count

    For i = 1 To x
    'Debug.Print ThisWorkbook.VBProject.References(i).Name
        'check if Solver reference is already referenced
        If LCase(ThisWorkbook.VBProject.References(i).Name) = "solver" Then
            MsgBox "Solver is already referenced"
            Exit Sub
        End If

    Next

    On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromFile "C:\Program Files\Microsoft Office\Office15\Library\SOLVER\SOLVER.XLAM"
    On Error GoTo 0
End Sub