excel 从Access中启动VBA代码求解热解

pftdvrlh  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(130)

我目前正在从Access VBA代码学习在Excel中启动求解器的基本功能,有以下问题。
请帮助我,因为我还没有找到任何解决方案,这在互联网上。
1.无论规划求解是否找到解,规划求解始终返回0。请参见
'运行分析1
1.的代码根本没有返回值
'运行分析2
1.当VBA代码在Access中时,如何在Excel中创建MsgBox?
谢谢你对我的支持。

Option Compare Database

Dim objXL As Excel.Application
Dim strText As String
Dim blnCheck As Boolean
Dim Zielwert As Single

Sub Solver()
'set reference to excel object library
'AddIns("solver add-in").Installed = True

Set objXL = CreateObject("Excel.Application")
strText = objXL.Application.LibraryPath

Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

'Zufallszahl zwischen 45 und 55
Zielwert = Int(40 + Rnd * (50 - 40 + 1))

With objXL
.Visible = True
.Application.DisplayAlerts = False
.Workbooks.Open (strText & "\solver\solver.xlam")
.Application.DisplayAlerts = True
.Workbooks("SOLVER.XLAM").RunAutoMacros 1
.Application.ScreenUpdating = True
Set wb = .Workbooks.Add
Set ws = .Sheets.Add

ws.Name = "Calculations"
ws.Range("A1:A8") = Int((Rnd * 5) + 1)
ws.Range("A8:A16") = Int((Rnd * 2) + 1)
ws.Range("B1") = Int((Rnd * 20) + 1)
ws.Range("A19").Formula = "=sum(A1:A16)"
ws.Range("B19") = "Zielwert " & Zielwert
ws.Range("B15") = "Soll >= " & ws.Range("B1").value
ws.Range("B16") = "Soll variabel "

.Sheets("Calculations").Activate

' reset
.Run "Solver.xlam!SolverReset"

' set up new analysis
.Run "SOLVER.XLAM!SolverOk", ws.Range("$A$19"), 3, Zielwert, ws.Range("$A$15:$A$16")

' add constraints
.Run "SOLVER.XLAM!SolverAdd", ws.Range("$A$15"), 3, "$B$1"

' run the analysis 1    - or
Result = .Run("SOLVER.XLAM!SolverSolve", True)

' run the analysis 2
' .Run "SOLVER.XLAM!SolverSolve", True, "ShowTrial"
' finish the analysis
.Run "SOLVER.XLAM!SolverFinish", KEEP
.Application.ScreenUpdating = True
.UserControl = True
.Visible = True

''''''''''''''''''''''''''''''''''''''''''''''''''''

' Value of Result is allways 0 anyway of Solver result
MsgBox Result
End With

With objXL
If ws.Range("$A$19") <> Zielwert Then
   'Result in Excel cell
   ws.Range("B3") = "Der Solver konnte kein Ergebnis berechnen"
   'Result in Access MsgBox. Hot to make this appear in Excel
   MsgBox "Der Solver konnte kein Ergebnis berechnen"
Else
   'Result in Excel cell
   ws.Range("B3") = "Der Solver konnte ein Ergebnis berechnen"
   'Result in Access MsgBox. Hot to make this appear in Excel
   MsgBox "Der Solver konnte ein Ergebnis berechnen"
End If
End With

'objXL.Quit

End Sub

Function ShowTrial(Reason As Integer)
   MsgBox Reason
   ShowTrial = 0
End Function

我尝试了两个版本的SOLVER.XLAM!SolverSolve”
'运行分析1'和'运行分析2

相关问题