select案例VBA空单元格

所以我有一个代码下面的一个select的情况下,围绕一组单元格的十进制值,根据input单元格的值给出不同的结果。 我想添加一个子句,以便如果其中一个input单元格为空,结果也是空的。

这是我的代码:

Sub JEeldepthoutlet() Dim score As Double, result As String Dim Rng As Range, i As Long i = 0 With Sheets("Velocity_Depth") For Each Rng In .Range("B12:B16") score = Rng.Value Select Case score Case Is >= 0.05 result = "1" Case Is >= 0.031 result = "0.6" Case Is >= 0.021 result = "0.3" Case Is >= 0 result = "0" End Select .Range("Q31").Offset(i).Value = result i = i + 1 Next Rng End With End Sub 

我试着添加以下两个选项,这两个选项都不起作用:

 Case Else result "" 

如果有一个空单元,这只给了我一个结果0

我也试过了

 Case "" Exit Sub 

这也给了我一个0的结果。

有没有人有任何想法? 提前致谢

使用变体

 Sub JEeldepthoutlet() Dim score As Variant, result As String Dim Rng As Range, i As Long i = 0 With Sheets("Velocity_Depth") For Each Rng In .Range("B12:B16") score = Rng.Value Select Case score Case Is = "" result = "" Case Is >= 0.05 result = "1" Case Is >= 0.031 result = "0.6" Case Is >= 0.021 result = "0.3" Case Is >= 0 result = "0" End Select .Range("Q31").Offset(i).Value = result i = i + 1 Next Rng End With End Sub 

将整个Select大小写包含在If语句中:

 Sub JEeldepthoutlet() Dim score As Double, result As String Dim Rng As Range, i As Long i = 0 With Sheets("Velocity_Depth") For Each Rng In .Range("B12:B16") score = Rng.Value If score <> "" Then Select Case score Case Is >= 0.05 result = "1" Case Is >= 0.031 result = "0.6" Case Is >= 0.021 result = "0.3" Case Is >= 0 result = "0" End Select Else result = "" End If .Range("Q31").Offset(i).Value = result i = i + 1 Next Rng End With End Sub 

将空单元格保存为数字variables时,将其存储为0。

为了避免我在Select Case之前添加了if语句

对于空单元格,我已经在case语句中使用的范围之外分配了一个值,在所有其他情况下,我都指定了单元格的值。 见下文:

 If ActiveCell = "" Then score = 101 Else score = ActiveCell End If Select Case score Case 0 To 35 Mark = "F" Comment = "Terrible - needs attention" Case 36 To 50 Mark = "D" Comment = "Needs attention" Case 51 To 65 Mark = "C" Comment = "Not bad, could do better" Case 66 To 85 Mark = "B" Comment = "Good score" Case 86 To 100 Mark = "A" Comment = "EXcelent score - well done!" Case Else Mark = "" Comment = "NO SCORE RECORDED" End Select