2013-08-23 49 views
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个ch​​arecters如果他们是小于7,并已工作了一一天,它突然停止了。我不断收到错误RUN TIME ERROR 6 OVERFLOW。我感到茫然,因为它一直工作到现在一直没有任何问题。它不断突出部分:

For i = 1 To endrow - 1 

有什么想法?

+0

你不需要“Do ... Loop”将0加到数字的前面。 'Activecell.value = right(Activecell.value,string $(7,“0”),7)'将在一个语句中这样做。另外,为了避免'select'语句,我会改变你的整个循环为:for i = 2 to endrow-1:cells(i,1)= right(cells(i,1),string $(7,“ 0“),7):next' – SeanC

回答

4

改变这一行:

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

为了这个:

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

整型变量只能上升到32,767。如果你的行号比这个更高,你需要使用Long。

+0

我感到非常愚蠢,谢谢你解决了这个问题。 – user2119980