2016-12-01 74 views
0

下面的代码有时它工作,为两个以上的值,它说的错误提一个溢出。 我只想写一个值的VBA代码的列在每个类别分开三类和计数数字。请告诉我这段代码的错误。VBA的Excel做,当且如果条件

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

你需要声明你的变量为多长时间......'Dim income As Long' – OpiesDad

+1

你需要观看[Excel VBA介绍第5部分 - 选择单元格(范围,单元格,活动单元格,结束,偏移)](https:// www.youtube.com/watch?v=c8reU-H1PKQ&index=5&t=3043s&list=PLNIs-AWhQzckr8Dgmgb3akx_gFMnpxTN5) – 2016-12-01 18:33:10

回答

2

的整数的最大值为32767。当然有收入高于此,特别是因为你在更高的检查值超过50K。声明所有的变量为多头:

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

,其余的应该是相同的。

+1

“剩下的应该是相同的” - 是啊。那么,除了垃圾缩进和'ActiveCell'和'.Select' ;-) –

+0

@ Mat'sMug哈哈哈依赖。非常真实。希望OP会查看问题评论中提供的链接。 :) – OpiesDad