突然的随机错误

时间:2013-08-23 18:40:13

标签: excel-vba vba excel

我有以下代码,

Sub AddZeroes()
'Declarations
Dim i As Integer, j As Integer, endrow As Long
'Converts the A column format to Text format
Application.ScreenUpdating = False
Columns("A:A").Select
Selection.NumberFormat = "@"
'finds the bottom most row
endrow = ActiveSheet.Range("A1").End(xlDown).Row
'selects the top cell in column A
ActiveSheet.Range("A1").Select
'loop to move from cell to cell
For i = 1 To endrow - 1
            'Moves the cell down 1. Assumes there's a header row so really starts at row 2
            ActiveCell.Offset(1, 0).Select
            'The Do-While loop keeps adding zeroes to the front of the cell value until it hits     a length of 7
Do While Len(ActiveCell.Value) < 7
                            ActiveCell.Value = "0" & ActiveCell.Value
            Loop
Next i
Application.ScreenUpdating = True
End Sub

它将前面的零添加到数字并将它们转换为文本,如果它们小于7则使它们长7个字符。它已经整天工作并突然停止了。我继续得到错误RUN TIME ERROR 6 OVERFLOW。我感到茫然,因为它一直没有任何问题,直到现在。它不断突出显示部分:

For i = 1 To endrow - 1

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

更改此行:

Dim i As Integer, j As Integer, endrow As Long

改为:

Dim i As Long, j As Long, endrow As Long

整数变量最多只能达到32,767。如果您的行号高于此行,则需要使用Long。