所以我有一个代码,用于围绕一组单元格的选择案例循环,其中十进制值根据输入单元格的值给出不同的结果。我试图添加一个子句,以便如果其中一个输入单元格为空,结果也为空。
这是我的代码:
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for kp2.keepy-i.com
Info: Applying configuration version '1463497694'
Notice: Applied catalog in 0.04 seconds
我尝试添加以下两个选项,但两个选项均无效:
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
如果有一个空单元格,这只给了我0的结果
我也试过
Case Else
result ""
这也给了我0的结果。
有没有人有任何想法?提前致谢
答案 0 :(得分:1)
将整个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
答案 1 :(得分:1)
使用变体:
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
答案 2 :(得分:0)
将空单元格保存为数字变量时,它将存储为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