2013-08-23 51 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