将Excel代码转换为c#

本文关键字:转换 代码 Excel | 更新日期: 2023-09-27 18:13:13

我有VBA在Excel中计算一些值。现在我必须在c#中实现这个技术。我试着去理解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

将Excel代码转换为c#

  1. 没有任何声明,所以你所有的变量都是类型variant

VBA:

k = 8
j = 35
...
Trials = Cells(4, j + 9).Value  
c#

:

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;  
  • SolverReset, SolverOkSolverAdd似乎是您没有提供代码的外接程序中的例程。所以没什么可做的…

  • For i = 1 To Trials在c#是for(int i = 1; i <= Trials; i++)