【问题标题】:The variable Stage is not taking any value for me变量 Stage 对我没有任何价值
【发布时间】:2021-11-06 06:16:11
【问题描述】:

我没有评估“等于”的逻辑运算符

Dim Stage As String
If Range("I5") = "si" Then
Stage = "E"
ElseIf Range("I5") = "no" And Range("I9") > 1020 And Range("I9") <= 3100 Then
Stage = "
Execution of leasehold adjustments to existing infrastructure for use and operation for 10 years."
ElseIf Range("I5") = "no" And Range("I9") > 3101 Then
Stage = "Construction and operation of a collection center for recyclable waste for 10 years."
ElseIf Range("I5") = "si" And Range("I9") <= 1020 Then
Stage = "Construction and operation of a collection center for recyclable waste for 10 years.."
ElseIf Range("I5") = "si" And Range("I9") > 1020 And Range("I9") <= 3100 Then
Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
ElseIf Range("I5") = "si" And Range("I9") > 3101 Then
Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
End If

他们知道我的代码有什么问题

【问题讨论】:

  • 单步执行,看看它在做什么。猜测If 声明都不是真的,所以它就结束了。
  • 如果I5 中包含si,则不会执行任何ElseIf 语句。
  • 请说明您是如何测试的,哪些输入值( Range("I5") 和 Range("I9") )给出了意外的结果( Stage )?
  • 按照 Warcupine 的建议,在第一个 If 处设置一个断点,然后逐步执行:google.com/search?q=vba+step+through+code

标签: excel vba if-statement


【解决方案1】:

在一个 if 语句中进行多个逻辑测试的问题之一是,您很容易对结果可能是什么感到困惑。您还犯了冗余测试的错误,这再次使您的代码混乱。

您可以很容易地将代码重构为嵌套的 if,这在一定程度上提高了它的可读性,更重要的是它的可测试性,因为修改后的代码一次只进行一个测试。我还加入了一些其他技巧,以使从电子表格中获取的测试值更加可靠。

Const mcSi As String = "si"
    Const mcNo As String = "no"
    
    Dim myI5 As String
    myI5 = VBA.Trim$(VBA.LCase$(ActiveSheet.Range("I5").Value))
    
    Dim myI9 As Long
    myI9 = ActiveSheet.Range("I9").Value
    
    Dim Stage As String
    If VBA.InStr(myI5, mySi) > 0 Then
    
            Stage = "E"  ' This E will be overwritten by one of the statement below
        
            If myI9 <= 1020 Then
        
                Stage = "Construction and operation of a collection center for recyclable waste for 10 years.."
                
            
            ElseIf myI9 <= 3100 Then
        
                Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
                
            
            Else
        
                Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
                
            
            End If
        
        
     Else
        
            If myI9 > 1020 Then
            
                If myI9 <= 3100 Then
                
                    Stage = "Execution of leasehold adjustments to existing infrastructure for use and operation for 10 years."
                    
            
                Else ' This is different to your code but I think youmay have missed out a test for I9 in your second No test
                
                    Stage = "Construction and operation of a collection center for recyclable waste for 10 years."
        
        
                End If
        
    End If

这让我们进入下一点,即嵌套的 if 几乎与 if 中的多个逻辑一样糟糕,因此下一步的简化是将 Si 和 No 的代码放入单独的函数中。当你这样做时,代码变得更容易理解和推理。

    Const mcSi As String = "si"
    Const mcNo As String = "no"
    
    Dim myI5 As String
    myI5 = VBA.Trim$(VBA.LCase$(ActiveSheet.Range("I5").Value))
    
    Dim myI9 As Long
    myI9 = ActiveSheet.Range("I9").Value
    
    Dim Stage As String
    
    If VBA.InStr(myI5, mySi) > 0 Then
    
        Stage = ContractRequirementsWhenI5IsSi(myI9)
        
    Else
    
    
        Stage = ContractRequirementsWhenI5IsNo(myI9)
        
    End If

Public Function ContractRequirementsWhenI5IsSi(ByVal ipI9Value As Long) As String

    Dim myResult As String
    
    Stage = "E"  ' This E will be overwritten by one of the statement below
    
    If myI9 <= 1020 Then

        Stage = "Construction and operation of a collection center for recyclable waste for 10 years.."
        
    
    ElseIf myI9 <= 3100 Then

        Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
        
    
    Else

        Stage = "Stage Simulation 1: Construction and operation of a collection center for recyclable waste for 10 years."
        
    
    End If
    
    ContractRequirementsWhenI5IsSi = myResult
    
End Function


Public Function ContractRequirementsWhenI5IsNo(ByVal ipI9Value As Long) As String

    Dim myResult As String
    
    If myI9 > 1020 Then
    
        If myI9 <= 3100 Then
        
            myResult = "Execution of leasehold adjustments to existing infrastructure for use and operation for 10 years."
            
    
        Else ' This is different to your code but I think youmay have missed out a test for I9 in your second No test
        
            myResult = "Construction and operation of a collection center for recyclable waste for 10 years."
    
    
        End If
        
    End If
    
    ContractRequirementsWhenI5IsNo = myResult
    
End Function

我希望以上内容能够让您正确设计您希望实施的测试集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多