2016-12-01 96 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