我试图在细胞中获得不同的结果" C8"取决于细胞的值之间的关系" B3"和" C3"
我首先尝试使用基本的Select Case:
Sub Salmonpool_depth1()
Dim score As Variant, result As String
With Sheets("Vertical")
score = Range("C3").Value
Select Case score
Case Is = ""
result = ""
Case Is >= 0.3 * Range("B3").Value
result = "0.3"
Case Is >= 0.6 * Range("B3").Value
result = "0.6"
Case Is >= Range("B3").Value
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
但它总是给我结果0.3作为结果如果任何条件被填充而不是最后一个条件,它确实在最后一个条件被填满时给我0。
然后我尝试将B3和C3定义为变量
Sub Salmonpool_depth2()
Dim pool As Variant, result As String, hydraulic As Variant
With Sheets("Vertical")
pool = Range("C3").Value
hydraulic = Range("B3").Value
Select Case pool
Case Is = ""
result = ""
Case Is >= 0.3 * hydraulic
result = "0.3"
Case Is >= 0.6 * hydraulic
result = "0.6"
Case Is >= hydraulic
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
但是这也给了我0.3或0的结果
然后我尝试使用If Then语句:
Sub Salmonpool_depth3()
Dim hydraulic As Variant, pool As Variant
hydraulic = Range("B3").Value
pool = Range("C3").Value
If pool >= hydraulic Then
Range("C8").Value = 1
End If
If pool >= 0.6 * hydraulic Then
Range("C8").Value = 0.6
End If
If pool >= 0.3 * hydraulic Then
Range("C8").Value = 0.3
End If
If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
End If
If pool = "" Then
Range("C8").Value = ""
End If
End Sub
但是这也给了我0.3或0。
有谁知道如何改变这个?由于程序不理解,因此我必须提出问题。
李楼
答案 0 :(得分:1)
Select Case语句中的问题是,一旦满足条件,它就会退出比较。所以当你的条件是......说0.8时,下面的代码说“它是否大于或等于0.3 *液压?是”然后从不比较0.6 *液压。
Case Is >= 0.3 * hydraulic
result = "0.3"
Case Is >= 0.6 * hydraulic
您需要对第一次比较设置限制,如下所示:
Case (0.3 * hydraulic) To (0.6 * hydraulic)
result = "0.3"
Case (0.6 * hydraulic) To 1
答案 1 :(得分:1)
问题是,如果它大于.03那么它将永远停在那里。当其中一个条件评估为尝试时,SELECT CASE语句退出。您需要以这样的方式订购它:
Case Is >= hydraulic
result = "1"
Case Is >= 0.6 * hydraulic
result = "0.6"
Case Is >= 0.3 * Range("B3").Value
result = "0.3"
答案 2 :(得分:0)
使用您发布的第一段代码,我认为您的&gt; =是倒退的。它应该是&lt; =。
Sub Salmonpool_depth1()
Dim score As Variant, result As String
With Sheets("Vertical")
score = Range("C3").Value
Select Case score
Case Is = ""
result = ""
Case Is <= (0.3 * Range("B3").Value)
result = "0.3"
Case Is >= (0.6 * Range("B3").Value)
result = "0.6"
Case Is >= Range("B3").Value
result = "1"
Case Else
result = "0"
End Select
Range("C8").Value = result
End With
End Sub
答案 3 :(得分:0)
我知道蒂姆,道格和JC都回答了你的问题,但正如你所说,你的if语句也给出了不正确的结果,我不确定是否有人解决了这个问题。如上所述,更改顺序将修复结果,但更优雅的解决方案是使用Else If语句。这样,你得到一个类似于使用Case的情况,它检查每个语句,直到一个是正确的,在该部分中执行代码部分,然后向前跳转到End If完成时。它看起来像这样:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
End If
这样,假设pool为0.7 * hydraulic,它将为第一个If语句抛出False,然后为下一个Else If行抛出True,将C8设置为0.6,然后移动到End If行。你的方式(在每个If语句之后放置End If)按顺序检查每个If语句,是否有东西通过了前一个语句。您还可以通过要求多个条件传递每个条件来使其更加健壮:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic And pool < hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic And pool < 0.6 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
End If
使用此snipet,您可以根据需要重新排序条件,并且只有在它落在特定范围内时才会通过。并且,如果您愿意,如果没有If或Else If语句通过,您可以在将运行的部分的末尾单独放置一条Else行:
If pool >= hydraulic Then
Range("C8").Value = 1
Else If pool >= 0.6 * hydraulic And pool < hydraulic Then
Range("C8").Value = 0.6
Else If pool >= 0.3 * hydraulic And pool < 0.6 * hydraulic Then
Range("C8").Value = 0.3
Else If pool < 0.3 * hydraulic Then
Range("C8").Value = 0
Else If pool = "" Then
Range("C8").Value = ""
Else
Range("C8").Value = "Invalid Entry"
End If