0

我有以下代码,

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

有什么想法吗?

4

1 回答 1

4

更改此行:

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

改为:

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

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

于 2013-08-23T18:42:33.327 回答