![]() | |||||||||||||||
| |||||||||||||||
Launching Mosel from Excel using VBA Description This example demonstrates how to launch Xpress Mosel from a VBA script within Microsoft Excel and retrieve the results of running a model. When a spreadsheet is open within Excel, Excel places a read-only lock on the file such that the mmsheet.xls|xlsx I/O drivers cannot write to the spreadsheet. This means that if you execute a model within Mosel, either from the command line console or via Xpress Workbench, the spreadsheet must be closed if you wish to export data from the model to the spreadsheet via the mmsheet.xls|xlsx I/O drivers (however, with the mmsheet.excel I/O driver it is possible to keep the spreadsheet file open while writing to it from Mosel). This example shows how to avoid this problem by retrieving the results into the spreadsheet using the VBA interface to Mosel, and then writing the data to the spreadsheet. Mosel's Excel interface is used by the model to read the input data from the spreadsheet, but is NOT used to output the results. Run the macro 'test' in the Excel spreadsheet to trigger several runs of the Mosel model with modified input data, displaying the results in the spreadsheet.
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
Data Files
excelmosel1.mos (!****************************************************** Mosel Example Problems ====================== file excelmosel1.mos ```````````````````` Small MIP problem reading data from Excel. (c) 2008 Fair Isaac Corporation rev. Jan. 2025 *******************************************************!) model "excelmosel1" uses "mmxprs" parameters DATA_XLS = 'excelmosel1.xls' end-parameters declarations ROWS = 1..4 COLS = 1..4 COEFFS: array(ROWS, COLS) of real ! Constraint coefficients COSTS: array(COLS) of real ! Objective coefficients RHS_p,RHS_v: array(ROWS) of real ! RHS terms public vars: array(COLS) of mpvar ! Decision variables Sol: array(COLS) of real ! Solution values end-declarations forall(j in COLS) vars(j) is_integer setparam("XPRS_VERBOSE", true) setparam("XPRS_LOADNAMES", true) ! Retrieve data from Excel initialisations from 'mmsheet.excel:'+ DATA_XLS COSTS as 'noindex;costs' COEFFS as 'coeffs' RHS_p as 'noindex;rhs_p' RHS_v as 'noindex;rhs_v' end-initialisations ! Define the optimization problem MaxObj:= sum(j in COLS) COSTS(j)*vars(j) forall(i in ROWS) sum(j in COLS) COEFFS(i,j) * vars(j) <= (RHS_p(i) + RHS_v(i)) * 10 ! Solve the problem maximize(MaxObj) writeln("MaxObj: ", getsol(MaxObj)) ! Save and display the solution forall(j in COLS) do Sol(j):= getsol(vars(j)) writeln("x", j, ": ", Sol(j)) end-do end-model
| |||||||||||||||
© Copyright 2025 Fair Isaac Corporation. |