If、Then、ElseIf 语句求解(使用求解器)我的两个条件并返回,但不会求解第三个条件。也没有错误信息

问题描述 投票:0回答:1

我正在尝试使用 VBA 求解一个变量以获得三种不同的回报:10%、16%、盈亏平衡。我必须使用求解器来避免循环引用,但我有 5 种不同的场景来解决所有三个标准。所以,我认为自动化求解器过程会大大加快速度。

这是我的代码:

Sub EntSolver()
    
    If Range("B8").Value = "0.16" Then
        Solverok SetCell:="E44", MaxMinVal:=3, ValueOf:=".16", ByChange:="E9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "0.10" Then
        
        Solverok SetCell:="E44", MaxMinVal:=3, ValueOf:=".1", ByChange:="E9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "Net Income B/E" Then
        
        Solverok SetCell:="E45", MaxMinVal:=3, ValueOf:="0", ByChange:="E9"
        SolverSolve (True)
        
    End If
        
    'find a solution by solving the problem
        
    If Range("B8").Value = "0.16" Then
        Solverok SetCell:="I44", MaxMinVal:=3, ValueOf:=".16", ByChange:="I9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "0.10" Then
        
        Solverok SetCell:="I44", MaxMinVal:=3, ValueOf:=".10", ByChange:="I9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "Net Income B/E" Then
        
        Solverok SetCell:="I45", MaxMinVal:=3, ValueOf:="0", ByChange:="I9"
        SolverSolve (True)
        
    End If
        
    'find a solution by solving the problem
        
    If Range("B8").Value = "0.16" Then
        Solverok SetCell:="M44", MaxMinVal:=3, ValueOf:=".16", ByChange:="M9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "0.10" Then
        
        Solverok SetCell:="M44", MaxMinVal:=3, ValueOf:=".10", ByChange:="M9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "Net Income B/E" Then
        
        Solverok SetCell:="M45", MaxMinVal:=3, ValueOf:="0", ByChange:="M9"
        SolverSolve (True)
      
    End If
        
    'find a solution by solving the problem
    If Range("B8").Value = "0.16" Then
        Solverok SetCell:="Q44", MaxMinVal:=3, ValueOf:=".16", ByChange:="Q9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "0.10" Then
        
        Solverok SetCell:="Q44", MaxMinVal:=3, ValueOf:=".10", ByChange:="Q9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "Net Income B/E" Then
        
        Solverok SetCell:="Q45", MaxMinVal:=3, ValueOf:="0", ByChange:="Q9"
        SolverSolve (True)
    
    End If
        
    If Range("B8").Value = "0.16" Then
        Solverok SetCell:="U44", MaxMinVal:=3, ValueOf:=".16", ByChange:="U9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "0.10" Then
        
        Solverok SetCell:="U44", MaxMinVal:=3, ValueOf:=".10", ByChange:="U9"
        SolverSolve (True)
        
    ElseIf Range("B8").Value = "Net Income B/E" Then
        
        Solverok SetCell:="U45", MaxMinVal:=3, ValueOf:="0", ByChange:="U9"
        SolverSolve (True)
    
    End If
        
        'find a solution by solving the problem
End Sub

单元格 B8 是一个数据验证下拉菜单,具有 10、16 和盈亏平衡% 选项。用户应该能够选择返回,然后运行代码。

它适用于 16% 和盈亏平衡选项,但由于某种原因,它不适用于 10% 选项。

我手动使用求解器找到 10% 变量,它有效,所以我不知道为什么它不起作用。如果您对发生的情况有任何了解,请提供帮助。非常感谢。

excel vba if-statement automation solver
1个回答
0
投票

很难说出确切的问题是什么,但这里有一些建议的更改,以及减少代码大小的方法(更少的行意味着更少的错误并且更容易进行更改......)

Sub EntSolver()
    
    Dim sel, ws As Worksheet, col, targ As Double, rw As Long
    
    Set ws = ActiveSheet 'for example
    
    sel = ws.Range("B8").Value
    
    Select Case sel    'what value to aim for, and which row to change?
        Case "0.16":
            targ = 0.16
            rw = 44
        Case "0.10"
            targ = 0.1
            rw = 44
        Case "Net Income B/E"
            targ = 0#
            rw = 45
        Case Else
            MsgBox "'" & sel & "' is not a valid value", vbExclamation
            Exit Sub
    End Select
    
    'loop over the different columns to be solved and run Solver
    For Each col In Array("E", "I", "M", "Q", "U")
        SolverReset
        Solverok SetCell:=ws.Cells(rw, col), MaxMinVal:=3, ValueOf:=targ, ByChange:=ws.Cells(9, col)
        SolverSolve (True)
    Next col
    
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.