VBA排名标准宏 - 否则没有错误

时间:2016-10-04 13:31:51

标签: vba excel-vba if-statement rank excel

我正在尝试根据单元格中的值创建一个返回给定排名的宏。它所基于的值(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

3 个答案:

答案 0 :(得分:1)

  1. 写&#34;结束&#34;在结果=范围之前的行(&#34; B26&#34;)...
  2. Dim score As Double,而不是整数,您的值太高而且它们不是整数。
  3. 这应该颠倒过来:

    范围(&#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)

至于其他人说的话:

  1. 如果使用Else或Else,则不能使用单行

  2. 得分必须是双倍

  3. 最后一行是相反的。

  4. 并添加

    1. 不需要少于参数,因为一旦if语句发现它就停止查看,就不需要了。
    2. 代码:

      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