Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

VBA macro example for excel

  • 1.  VBA macro example for excel

    Posted 02/21/11 06:05 PM

    Originally posted by: FidanBoylu


    where can I find a simple VBA macro code example that uses cplex solver and is built in functions?
    I have already installed cplex add-on to Excel, all I need is a VBA code example that shows how to set the variables, objective function and add constraints. thanks in advance!
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: VBA macro example for excel

    Posted 02/22/11 03:03 AM

    Originally posted by: SystemAdmin


    Assuming your CPLEX directory is C:\path\to\cplex, there is a directory C:\path\to\cplex\examples\src\excel. This contains example1.bas, example2.bas and example3.bas. All examples illustrate how to use the functions defined in C:\path\to\cplex\excel\cplexvba.bas.
    It may even be sufficient to read the comments in cplexvba.bas.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: VBA macro example for excel

    Posted 02/22/11 06:37 AM

    Originally posted by: FidanBoylu


    I found them and will look. Thank you very much. I will be back if any questions!
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: VBA macro example for excel

    Posted 02/23/11 10:14 AM

    Originally posted by: FidanBoylu


    Hi I am getting an error "Incorrect formula in cell R20C12 (where the objective function is). Probably the objective function is too long, try to split it up." After that, "Error 10: Failed to parse formula."

    Would you have a possible explanation?
    I have a long linear objective function with binary variables. There is about 15 binary variables in total for the model. I was hoping to solve a way bigger problem woth 300 binary variables, but even this small prototype doesn't seem to work. I am using academic initative version, excel add-in.

    than you very much!!!
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: VBA macro example for excel

    Posted 02/23/11 10:38 AM

    Originally posted by: SystemAdmin


    The best thing you can do is to do what CPLEX suggested: Split up the formula.
    What causes trouble is not the number of cells you are referencing in the formula but the textual representation of the formula. For example:
    =A1 + A2 + A3 + ... + A25
    

    may fail while
    =SUM(A1:A25)
    

    will work.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: VBA macro example for excel

    Posted 02/23/11 10:16 AM

    Originally posted by: FidanBoylu


    new question.
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: VBA macro example for excel

    Posted 06/13/12 10:04 AM

    Originally posted by: FidanBoylu


    Hi,
    I need your help again. I am trying to solve a larger size model with cplex excel add-in where I have about 10,000 constraints and 1000 variables. The problem is that cplex window is not loading all the constraints in the vb code. it seems to stop adding constraints after some number which is way less than 10,000... I am wondering if there is a maximum number of constraints that can be entered with the add-in (I hope not). I know cplex can solve huge models, would excel add-in cause any problems in that regard? what could be the problem?
    I appreciate your help.
    thanks!
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: VBA macro example for excel

    Posted 06/13/12 11:37 AM

    Originally posted by: SystemAdmin


    Are you adding the constraints via VBA macros or using the GUI? Do you add the constraints individually or as a set of constraints?
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: VBA macro example for excel

    Posted 06/13/12 12:10 PM

    Originally posted by: FidanBoylu


    I am adding constraints using CPXaddConstraint in the VBA macro.
    thanks for the quick reply!
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: VBA macro example for excel

    Posted 06/13/12 12:11 PM

    Originally posted by: FidanBoylu


    I add the constraints individually, would that be why?
    Using ranges would be a better option I guess, correct?
    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: VBA macro example for excel

    Posted 06/13/12 06:48 PM

    Originally posted by: FidanBoylu


    OK, I added some of my constraints as ranges and I was able to load more constraints now. However, there is a good deal of variables and constraints that cannot be added as ranges (single cells that I can not really do anything about). Can you provide some insight about what seems to be the limits and any recommendations about what can be done?
    Thank you!!!
    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: VBA macro example for excel

    Posted 06/17/12 09:07 AM

    Originally posted by: SystemAdmin


    I don't know of any hard-coded limits.
    Do you check the return value of CPXaddConstraint()? Does it always return non-zero or does it start returning 0 at some point? What is the last non-zero value that is returned by the macro?
    #CPLEXOptimizers
    #DecisionOptimization


  • 13.  Re: VBA macro example for excel

    Posted 06/17/12 11:35 AM

    Originally posted by: FidanBoylu


    Daniel,
    I kinda got over those earlier problems, now all the constraints of the model load but now I am getting a cplex engine error for some constraint cells that looks like for example:
    (1181,21):Error 6: CPLEX engine error.

    What is error 6???

    thanks
    #CPLEXOptimizers
    #DecisionOptimization


  • 14.  Re: VBA macro example for excel

    Posted 06/17/12 12:25 PM

    Originally posted by: SystemAdmin


    Error 6 is the "engine error". Do you get this when adding constraints or when solving the model? (1181,21) are the coordinates of the offending cell. Is there anything suspicious about this cell's content?
    #CPLEXOptimizers
    #DecisionOptimization


  • 15.  Re: VBA macro example for excel

    Posted 06/17/12 02:39 PM

    Originally posted by: FidanBoylu


    I get it when solving the model after CPXsolve. Yes, those are the coordinates of the offending cell.
    Now, I discovered that the offending cell changes for different sizes of the model as more variables and constraint are added and the content of the offending cell varies accordingly. I also tried to solve it for a very small size problem, it works fine!... I am still suspecting that this is some sort of size violation but you'd probably understand better...
    please help!:)
    #CPLEXOptimizers
    #DecisionOptimization


  • 16.  Re: VBA macro example for excel

    Posted 06/17/12 04:31 PM

    Originally posted by: FidanBoylu


    Also, the offending cell is not necessarily a constraint cell, sometimes it is some intermediate cell for calculations of a constraint...I don't know if that helps or not..
    #CPLEXOptimizers
    #DecisionOptimization


  • 17.  Re: VBA macro example for excel

    Posted 06/17/12 06:33 PM

    Originally posted by: FidanBoylu


    One more observation, when I close and reopen the excel file and solve exactly the same model, I get different offending cells.
    I really really appreciate your quick help.
    #CPLEXOptimizers
    #DecisionOptimization


  • 18.  Re: VBA macro example for excel

    Posted 06/17/12 10:14 PM

    Originally posted by: FidanBoylu


    Why would you get an engine error in the first place? I couldn't find any information on this error for some reason...it is killing me...can you please put some ideas when you get a chance?
    #CPLEXOptimizers
    #DecisionOptimization


  • 19.  Re: VBA macro example for excel

    Posted 12/30/15 01:02 AM

    Originally posted by: nima_salehi


    Hi,

    I am trying to run the CPLEX engine for a model that I created in Excel VBA. It is a big model and I cannot assign one cell to every variable I have. So my question is: Is it possible for my to define variables in VBA for the parameters of my model and then choose some cell as the variables of my model and define the constraints and the objective in the VBA code instead of the Excel sheet?

    Here is what I did so far (it is not working):

     


        Dim BinVars As Range
        Set BinVars = Range(Worksheets("Sheet2").Cells(3, 2), Worksheets("Sheet2").Cells(5, 2))
        
        'CPXaddVariable Variable:=BinVars, Binary:=True
        
        Dim CapVars As Range
        Set CapVars = Range(Worksheets("Sheet2").Cells(3, 4), Worksheets("Sheet2").Cells(5, 14))
        
        CPXaddVariable Variable:=CapVars, Binary:=True
        
        Dim Flows() As Variant          'I have too many variables in this group and I cannot assign a cell for each of them
      ReDim Flows(1 To n, 1 To m, 1 To l) As Variant
        
        CPXaddVariable Variable:=Flows, Lb:=0
        
        Dim ObjCoefs1() As Double
        ReDim ObjCoefs1(1 To n, 1 To m) As Double

        Dim ObjCoefs2() As Double
        ReDim ObjCoefs2(1 To n, 1 To m, 1 To l) As Double
        
            For i = 1 To n
                For j = 1 To m
                    TotalCost1 = TotalCost1+ sc(i).Coef1(j) * CapVars(i, j)
                    
                    For k = 1 To l
                        TotalCost2 = TotalCost2 + sc(i).Coef2(j) * Flows(i, j, k)
                    Next k
                Next j
            Next i
            
            Total = TotalCost1 + TotalCost2 
            Worksheets("Sheet2").Cells(13, 19).Value = Tot

        CPXsetObjective ObjCell:=Worksheets("Sheet2").Cells(13, 19), Sense:=2

     

        ' Solve the problem.
        CPXsolve


    #CPLEXOptimizers
    #DecisionOptimization


  • 20.  Re: VBA macro example for excel