我正在尝试根据单元格中的值创建一个返回给定排名的宏。它所基于的值(B24)将决定排名并将其置于B26中。以下是我正在使用的排名和代码。防爆。超过2B的值应该导致" 1"。我怎样才能让它发挥作用?我目前正在"编译错误:否则没有"
Sub Criteria()
On Error GoTo catch_error
Worksheets("Sheet1").Activate
Dim score As Integer, result As String
score = Range("B24").Value
If score > 2000000000 Then result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then result = "4"
ElseIf score < 249999999.99 Then result = "Out Of Scope"
result = Range("B26").Value
Exit Sub
catch_error:
MsgBox "Some Error Occurred"
End Sub
答案 0 :(得分:1)
Dim score As Double
,而不是整数,您的值太高而且它们不是整数。这应该颠倒过来:
范围(&#34; B26&#34;)。值=结果
答案 1 :(得分:1)
单行If
语句无法Else
:
If score > 2000000000 Then result = "1"
你需要像这样重组它:
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 And score <= 1999999999.99 Then
result = "2"
ElseIf score >= 500000000 And score <= 1499999999.99 Then
result = "3"
ElseIf score >= 250000000 And score <= 499999999.99 Then
result = "4"
ElseIf score < 249999999.99 Then
result = "Out Of Scope"
End If
答案 2 :(得分:1)
至于其他人说的话:
如果使用Else或Else,则不能使用单行
得分必须是双倍
最后一行是相反的。
并添加
代码:
Sub Criteria()
On Error GoTo catch_error
With Worksheets("Sheet1")
Dim score As Double, result As String
score = .Range("B24").Value
If score > 2000000000 Then
result = "1"
ElseIf score >= 1500000000 Then
result = "2"
ElseIf score >= 500000000 Then
result = "3"
ElseIf score >= 250000000 Then
result = "4"
Else
result = "Out Of Scope"
End If
.Range("B26").Value = result
Exit Sub
End With
catch_error:
MsgBox "Some Error Occurred"
End Sub