0

请协助运行以下 Excel 宏。我想我错过了一些东西。逻辑不行。

Sub RiskGrade()

Dim Value1 As String

Sheets("Sheet1").Select
If (Cells(10, 2) = "BX") Then Cells(12, 4) = -8
Exit Sub

ElseIf (Cells(10, 2) = "GX") Then Cells(12, 4) = -7
Exit Sub

Else
    For i = 12 To 14

        Value1 = Right(Trim(Sheet1.Cells(i, 2)), 1)
        'MsgBox Value1
        Select Case Value1
            Case "W"
                Cells(i, 4) = 1
            Case "H", "S", "R", "F", "G"
                Cells(i, 4) = 2
            Case "D"
                Cells(i, 4) = 3
            Case "C"
                Cells(i, 4) = 4
            Case "B"
                Cells(i, 4) = 5
            Case "A"
                Cells(i, 4) = 6
            Case "*"
                Cells(i, 4) = 7
            Case "M"
                Cells(i, 4) = 8
            Case "E"
                Cells(i, 4) = 9
            Case OTHER
                Cells(i, 4) = -9
        End Select
        Exit For
    Next i
End If

End Sub
4

4 回答 4

9

您已经启动了一个 IF 语句并在同一行中添加了一个操作,这意味着它不会在此之后期待一个 END IF 或 ELSE。

在 VB 中

IF 条件 THEN 动作

是单个 IF 语句。

考虑

If (Cells(10, 2) = "BX") Then
  Cells(12, 4) = -8
  Exit Sub
ElseIf (Cells(10, 2) = "GX") Then
  Cells(12, 4) = -7
  Exit Sub
...
于 2012-09-24T18:09:05.147 回答
3
  1. Case OTHER应该Case Else
  2. Exit For在第一次通过后终止循环,因此需要将其删除或更改为您希望处理中止的位置
  3. If ... Then应该是多行或单行。

    • 多行:
      If (Cells(10, 2) = "BX") Then
      Cells(12, 4) = -8
      Exit Sub
      ElseIf ...

    • 单行:( 注意在同一语句中使用分隔命令)
      If (Cells(10, 2) = "BX") Then Cells(12, 4) = -8 : Exit Sub
      ElseIf ...

      :If

于 2012-09-24T18:47:06.953 回答
0

您应该使用OPTION EXPLICIT,因为它有助于在将拼写错误应用于关键字时发现错误。

在当前形状中,您使用Case OTHERwhich should be指定了意外行为,VBACase Else中没有关键字,因此被解释为数据类型的变量,但会被正确解释为“在其他情况下做......”。OTHEROTHERVariantCase Else

同样使用Exit Forafter Selectstop 宏执行 on i=12, 13 和 14 然后不应用。

Option Explicit
Sub RiskGrade()

Dim Value1 As String

Sheets("Sheet1").Select

If (Cells(10, 2) = "BX") Then 
Cells(12, 4) = -8
Exit Sub

ElseIf (Cells(10, 2) = "GX") Then 
Cells(12, 4) = -7
Exit Sub

Else
    For i = 12 To 14

        Value1 = Right(Trim(Sheet1.Cells(i, 2)), 1)
        Debug.Print Value1 'CTRL+G for immediate window

        Select Case Value1
            Case "W"
                Cells(i, 4) = 1
            Case "H", "S", "R", "F", "G"
                Cells(i, 4) = 2
            Case "D"
                Cells(i, 4) = 3
            Case "C"
                Cells(i, 4) = 4
            Case "B"
                Cells(i, 4) = 5
            Case "A"
                Cells(i, 4) = 6
            Case "*" 'I don't know what behaviour in will cause, does it spot '*' only, or any char
                Cells(i, 4) = 7
            Case "M"
                Cells(i, 4) = 8
            Case "E"
                Cells(i, 4) = 9
            Case Other ' not OTHER
                Cells(i, 4) = -9
        End Select
        'Exit For ' - why exit after first iteration - guess it's unnecessary procedure ?
    Next i
End If

结束子

于 2014-12-17T12:05:35.930 回答
-1

如果您同时删除“退出子”,它应该可以工作。使用“exit sub”,您似乎正在关闭您的 if 没有“end if”

于 2014-02-15T01:06:32.610 回答