我试图运行这个vba任何建议

时间:2014-04-02 05:35:23

标签: vba

我是这个菜鸟的任何建议

Private Sub CommandButton1_Click()
Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
    If HG = A Then
     Select Case LU
    Case Is = 1
        CN = 77
    Case Is = 3
        CN = 67
    Case Is = 4
        CN = 30
    Case Is = 5
        CN = 39
    Case Is = 6
        CN = 49
    Case Is = 7
        CN = 77
    Case Is = 12
        CN = 98
    Case Else
        CN = 0
    End Select
ElseIf HG = B Then
   Select Case LU
    Case Is = 1
        CN = 85
    Case Is = 3
        CN = 78
    Case Is = 4
        CN = 55
    Case Is = 5
        CN = 61
    Case Is = 6
        CN = 62
    Case Is = 7
        CN = 86
    Case Is = 12
        CN = 98
    Case Else
        CN = 0
    End Select
ElseIf HG = C Then
   Select Case LU
    Case Is = 1
        CN = 90
    Case Is = 3
        CN = 85
    Case Is = 4
        CN = 70
    Case Is = 5
        CN = 74
    Case Is = 6
        CN = 74
    Case Is = 7
        CN = 91
    Case Is = 12
        CN = 98
    Case Else
        CN = 0
    End Select
ElseIf HG = D Then
   Select Case LU
    Case Is = 1
       CN = 92
    Case Is = 3
        CN = 89
   Case Is = 4
        CN = 77
   Case Is = 5
        CN = 80
    Case Is = 6
        CN = 85
    Case Is = 7
        CN = 94
    Case Is = 12
        CN = 98
    Case Else
        CN = 0
    End Select
ElseIf HG = NA Then
        CN = 98
End If

Range("G1").Value = CN
End Sub

1 个答案:

答案 0 :(得分:1)

查看您的变量声明,您似乎希望代码基于单元格值运行。

Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
    If HG = A Then
    Select Case LU

我猜你没有在模块顶部使用Option Explicit指令,因此VBA将A视为变量而不是字符串。我想你想要这个:

Dim LU As Double, HG As String, CN As Double
LU = Range("B1").Value
HG = Range("G1").Value
    If HG = "A" Then
     Select Case LU

如果那是正确的,那么当你拉出范围值时,你应该注意使用Trim $(除非你不希望代码在A上跟随空格匹配)。

您还想启用Option Explicit。它将有助于在代码中捕获这些类型的错误。