Convert Excel Macro in to C#


hello everyone,
               have macro in excel calculates values, have implement technique used in macro in c# application.

tried understand macro haven't work in excel

can expert me convert following macro in c#.

sub singleln()
'
' singleln macro
'

k = 8
j = 35

'reset trial number , time
cells(4, j + 12).value = ""
cells(5, j + 12).value = ""
cells(6, j + 12).value = ""


cells(5, j + 12).value = now
trials = cells(4, j + 9).value
= 1 trials
cells(4, j + 12).value = i
              
    solverreset
               
           
            cells(4, 5).value = (18 - 0.5) * rnd + 0.5
            cells(5, 5).value = (4 - 0) * rnd + 0
                                   
            cells(k + i, j + 1).value = cells(4, 5).value
            cells(k + i, j + 2).value = cells(5, 5).value
            
            cells(k + i, j + 9).value = cells(2, 21).value
            cells(k + i, j + 10).value = i


    solverok setcell:="$u$2", maxminval:=1, valueof:="0", bychange:= _
        "$e$3:$e$5,$g$4:$g$5"
    solveradd cellref:="$e$3", relation:=3, formulatext:="0"
    solveradd cellref:="$g$3", relation:=3, formulatext:="0"
    solveradd cellref:="$e$4", relation:=3, formulatext:="0"
    solveradd cellref:="$g$4", relation:=3, formulatext:="0"
    solveradd cellref:="$e$5", relation:=3, formulatext:="0"
    solveradd cellref:="$g$5", relation:=3, formulatext:="0"
    solveradd cellref:="$e$3", relation:=1, formulatext:="1"
    solveradd cellref:="$g$3", relation:=1, formulatext:="1"
    solveroptions maxtime:=100, iterations:=1000, precision:=0.000001, assumelinear _
        :=false, stepthru:=false, estimates:=1, derivatives:=1, searchoption:=1, _
        inttolerance:=5, scaling:=false, convergence:=0.0001, assumenonneg:=true
    solversolve userfinish:=true
    solverfinish keepfinal:=1
        
    result = application.run("solver.xlam!solversolve", true)

  ' finish analysis
    application.run "solver.xlam!solverfinish"
        
        
        cells(k + i, j + 12).value = cells(4, 5).value
        cells(k + i, j + 13).value = cells(5, 5).value
                       
        cells(k + i, j + 20).value = cells(2, 21).value
        cells(k + i, j + 21).value = cells(5, 14).value
          
        ' report on success of analysis
            'if result = 0 then
            'cells(k + i, j + 22).value = "solution found, optimality , constraints satisfied"
            
            'elseif result = 1 then
            'cells(k + i, j + 22).value = "solution found, converged, constraints satisfied"
            
            'elseif result = 2 then
            'cells(k + i, j + 22).value = "solution found, cannot improve, constraints satisfied"
            
            'elseif result = 3 then
            'cells(k + i, j + 22).value = "solution found, stopped @ maximum iterations"
            
            'else
              ' result = 4, solver did not converge
              ' result = 5, no feasible solution
            'cells(k + i, j + 22).value = "no solution"
            'end if
       cells(k + i, j + 22).value = result
    next i
cells(6, j + 12).value = now
end sub



www.techgulf.blogspot.com

first need way open excel file code.

if need support .xlsx, can use open xml sdk. or ziparchive , xmlreader classes.

if need support older .xls format, way use office interop. wich invovles handling com libraries , needs office installed.

about remaking macro:

without understanding macro does, there no way replicate it. have @ least clear, human language explanation does? context used in?
might need ask excel specialist wth code doing. after know, can think coding in c#.

as far can tell actuall work not done macro. seems relay work application in line:
result = application.run("solver.xlam!solversolve", true)

.xlam seems excel add-in. seems put cells in selection, call addin actuall work. , without addins code can't replicate @ in c#.


Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'