将Excel代码转换为C#

时间:2015-09-18 10:18:20

标签: c# excel vba

我在Excel中有VBA来计算一些值。现在我必须将技术实现到C#中。我试图理解VBA,但却无法理解VBA。因为我还没有完成Excel的工作。

任何专家都可以帮我将以下内容转换为C#吗?

Sub SingleLN()
'
' SingleLN Macro
'

k = 8
j = 35

'Reset Trial number and 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
For i = 1 To 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 the 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 and 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 at 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

1 个答案:

答案 0 :(得分:1)

  1. 没有声明任何内容,因此您的所有变量都是k = 8 j = 35 ... Trials = Cells(4, j + 9).Value
  2. 类型

    VBA:

    var k;
    var j;
    var Trials;
    
    // example, the cells methods will need to be fully qualified properly.
    k = 8;
    j = 35;
    Trials = sh.Cells[4, j + 9].Value2;  
    

    C#:

    SolverReset
    1. SolverOkSolverAddFor i = 1 To Trials似乎是您未提供代码的加载项中的例程。所以没那么多......

    2. C#中的
    3. for(int i = 1; i <= Trials; i++)是{{1}}

    4. 其他一切都很标准。