VBA Excel执行while和If条件

时间:2016-12-01 18:16:14

标签: vba

下面的代码有时会起作用,对于两个以上的值,它会说出一个提示溢出的错误。 我只想为列中的值编写VBA代码,以分隔三个类别并计算每个类别中的数字。请告诉我这段代码的错误。

Sub income_status()

Dim income As Integer
Dim locount As Integer
Dim mecount As Integer
Dim hicount As Integer



Do While ActiveCell.Value <> ""

    income = ActiveCell.Value


    If income <= 10000 Then
    ActiveCell.Offset(0, 1).Value = "Low Income"
    locount = locount + 1

    ElseIf income > 10000 And income <= 50000 Then

    ActiveCell.Offset(0, 1).Value = "Medium Income"
    mecount = mecount + 1

    Else

    ActiveCell.Offset(0, 1).Value = "High Income"
    hicount = hicount + 1


    End If
    ActiveCell.Offset(1).Select

  Loop
    ActiveCell.Offset(1, 2).Value = locount
    ActiveCell.Offset(1, 2).Value = mecount
    ActiveCell.Offset(1, 2).Value = hicount


End Sub

1 个答案:

答案 0 :(得分:2)

整数的最大值是32767.肯定有高于此的收入,特别是因为您正在检查高于50k的值。将所有变量声明为long:

Dim income As Long
Dim locount As Long
Dim mecount As Long
Dim hicount As Long

其余应该是相同的。