使用Select Case或If Then VBA比较两个单元格

我试图根据单元格“B3”和“C3”的值之间的关系,在单元格“C8”中获得不同的结果

我第一次尝试了一个基本的select案例:

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作为variables来尝试

 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以上。

有没有人有任何想法如何改变这一点? 当程序不理解的时候,我一定要问这个问题。

李楼

问题是,如果它大于.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" 

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 

使用你发布的第一个代码,我认为你的代码是倒退的。 它应该是<=。

 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 

我知道蒂姆,道格和JC都已经回答了你的问题,但是正如你所说的,如果你的陈述也给出了不正确的结果,我不确定是否有人解决了这个问题。 正如所写,更改顺序将修复结果,但更优雅的解决scheme是使用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 

这样,可以说池为0.7 *液压,它会为第一个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