当条件标准未满足时,IF语句评估为“真”

时间:2017-12-08 19:30:02

标签: excel-vba vba excel

嗨,大家好。

我正在尝试创建一个计算器,根据多种气体的值确定要采取的操作。每种气体在一个单元内的值可以在四个范围之一内。每种气体的“优先等级”是其值所在的范围。然后,最高优先级确定要采取的操作。

问题:

即使单元格中的值大于条件中指定的值,每个条件块的第一个If语句也会被评估为“True”。如何根据单元格中的值来获取条件语句以正确评估条件。

Sub DGOA_Action_500kVA()

Dim HydrogenValue As Variant
Dim MethaneValue As Variant
Dim EthaneVaule As Variant
Dim EthyleneValue As Variant
Dim AcetyleneValue As Variant
Dim Action As Integer


Dim HydrogenPriority As Integer
Dim MethanePriority As Integer
Dim EthanePriority As Integer
Dim EthylenePriority As Integer
Dim AcetylenePriority As Integer

Worksheets("500 kVa").Activate
HydrogenValue = Worksheets("500 kVa").Range("B3").Select
MethaneValue = Worksheets("500 kVa").Range("C3").Select
EthaneVaule = Worksheets("500 kVa").Range("F3").Select
EthyleneValue = Worksheets("500 kVa").Range("G3").Select
AcetyleneValue = Worksheets("500 kVa").Range("H3").Select


Dim PriorityRange As Range
Set PriorityRange = Worksheets("500 kVa").Range("J3:P3")


If HydrogenValue < 2000 Then
    HydrogenPriority = 1
    MsgBox (Worksheets("500 kVa").Range("B6").Select)

    ElseIf HydrogenValue >= 2000 And HydrogenValue < 20000 Then
        HydrogenPriority = 2

    ElseIf HydrogenValue >= 20000 And HydrogenValue < 27000 Then
        HydrogenPriority = 3

    ElseIf HydrogenValue >= 27000 Then
        HydrogenPriority = 4
End If


If MethaneValue < 2000 Then
    MethanePriority = 1

    ElseIf MethaneValue >= 2000 And MethaneValue < 15000 Then
        MethanePriority = 2

    ElseIf MethaneValue >= 15000 Then
        MethanePriority = 4
End If


If EthaneVaule < 1000 Then
    EthanePriority = 1

    ElseIf EthaneVaule >= 1000 And EthaneVaule < 2250 Then
        EthanePriority = 3

    ElseIf EthaneVaule <= 2250 Then
        EthanePriority = 4
End If

If EthyleneValue < 1000 Then
    EthylenePriority = 1

    ElseIf EthyleneValue >= 1000 And EthyleneValue < 3000 Then
        EthylenePriority = 3

    ElseIf EthyleneValue >= 2250 Then
        EthylenePriority = 4

    Else
        EthylenePriority = 1
End If

If AcetyleneValue < 1 Then
    AcetylenePriority = 1

    ElseIf AcetyleneValue >= 1 And AcetyleneValue < 20 Then
        AcetylenePriority = 2

    ElseIf AcetyleneValue >= 20 And AcetyleneValue < 100 Then
        AcetylenePriority = 3

    ElseIf AcetyleneValue >= 100 Then
        AcetylenePriority = 4

    Else
        AcetylenePriority = 1

End If

Worksheets("500 kVa").Range("J3").Value = HydrogenPriority
Worksheets("500 kVa").Range("K3").Value = MethanePriority
Worksheets("500 kVa").Range("N3").Value = EthanePriority
Worksheets("500 kVa").Range("O3").Value = EthylenePriority
Worksheets("500 kVa").Range("P3").Value = AcetylenePriority



Action = Application.WorksheetFunction.Max(PriorityRange)

Select Case Action

    Case 1
        Worksheets("Sheet1").Range("AX4").Value = "Normal"
    Case 2
        Worksheets("Sheet1").Range("AX4").Value = "Add in Watch List"
    Case 3
        Worksheets("Sheet1").Range("AX4").Value = "Resample"
    Case 4
        Worksheets("Sheet1").Range("AX4").Value = "Engineering Evaluation"
    Case Else
        Worksheets("Sheet1").Range("AX4").Value = "Error"
End Select

Worksheets(1).Activate
MsgBox ("The Action Has Been Determined")

End Sub

1 个答案:

答案 0 :(得分:0)

我意识到我偶然使用“选择”属性而不是“值”属性来获取单元格中的内容。

问题解决了