Case只返回Else声明

时间:2017-10-13 21:17:31

标签: vba excel-vba excel

当我运行我的案例时,它只返回每行的Else语句。我知道这个案例应该有效,因为当我在像“D2”这样的单元格上测试时,它只返回了正确的数据。只有当我尝试在一个循环中使用这个案例时,我才会得到全零。我尝试过研究几件事情,但却无法使代码正常工作。但是我对VBA很新,因为在接收这个项目之前,我对JavaScript有一些轻松的经验......请帮助。

Dim account As Range
Dim i As Long

Set account = Range("D2:D1100")
For Each cell In account

    If cell.Value Then

        Dim acctNum As Integer, result4 As Integer

            Select Case acctNum

               Case 1110
                    result = 105010

                Case 2710
                    result = 205000

                Case 2750
                    result = 205010

                Case 3110
                    result = 401100

                Case 3115
                    result = 401110

                Case 3120
                    result = 401120

                Case Else
                    result = 0

            End Select
        cell.Offset(0, 1).Value = result
    End If
Next

4 个答案:

答案 0 :(得分:1)

您必须为变量acctNum分配一个值。现在,您只需创建变量,使其在使用时没有任何值。见下文。

Dim acctNum As Integer, result4 As Integer ' You've created the variable here

Select Case acctNum 'And here you're trying to use it but it hasn't been given a value yet

相反,如果您在Select Case部分之前给它一个值,您的代码将提供您正在寻找的结果:

Dim acctNum As Integer, result4 As Integer ' You've created the variable here

acctNum = 2710

Select Case acctNum 'And now the variable has a value and should hit the '2710' case

希望这会有所帮助:)

答案 1 :(得分:0)

好像你想要这样的东西:

Dim account As Range
Dim i As Long
Dim acctNum As Integer, result4 As Integer

Set account = Range("D2:D1100")

For Each cell In account.Cells

    If Len(cell.Value) > 0 Then
        Select Case cell.Value
            Case 1110
                result = 105010
            Case 2710
                result = 205000
            Case 2750
                result = 205010
            Case 3110
                result = 401100
            Case 3115
                result = 401110
            Case 3120
                result = 401120
            Case Else
                result = 0
        End Select
        cell.Offset(0, 1).Value = result
    End If
Next

答案 2 :(得分:0)

我修改了下面的代码,它按预期工作。我建议使用数组来完成这类工作,但是当你对VBA更加满意的时候,这会让你有所了解。

其他人已经指出了你所犯的错误,我修了一些你以后会发现的错误。

对于超过32000的值,您希望使用Long而不是Integer(它不是32000,但如果您接近该数字,则只需使用Long)

我不认为使用acctNum是值得的,所以我把它拿出来,然后你昏暗'但'然后不要使用它,所以我也删除了它。

Dim account As Range
Dim result As Long

Set account = Range("D2:D1100")
For Each cell In account

    If cell.Value Then

        Select Case cell.Value

            Case 1110
                result = 105010

            Case 2710
                result = 205000

            Case 2750
                result = 205010

            Case 3110
                result = 401100

            Case 3115
                result = 401110

            Case 3120
                result = 401120

            Case Else
                result = 0

        End Select
    cell.Offset(0, 1).Value = result
End If
Next

答案 3 :(得分:0)

帮助您了解可能存在误解的地方。

如果您说Select Case acctNum,则表示每个人caseacctNum基于True/ False进行评估。例如:

1110True评估为acctNum吗?不......那么下一个案例。

2710True评估为acctNum吗?不......等等,直到你发表其他陈述。

这些评估为False的原因是因为您尚未为acctNum分配值,因此您

如上面的答案中所述,如果您更改了您正在评估的内容,您将获得预期的输出。

Select Case cells.value - 现在,您正在针对cases范围内的细胞评估account