VBA代码中的调试错误
我有下面的代码,它将零添加到一个数字中,直到这个数字总共有7位数。到目前为止,它运行良好,代码是:VBA代码中的调试错误,vba,Vba,我有下面的代码,它将零添加到一个数字中,直到这个数字总共有7位数。到目前为止,它运行良好,代码是: 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.NumberForma
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
我似乎想不出哪里出了问题。这个部分告诉宏,一旦它到达列表的末尾,查找空白空间并向上移动1,它就在最后一个数字上停止,并且它一直工作到今天。 您使用的是一个整数值,用于<代码> i <代码>,这可能导致该变量溢出。 试试这个:
Option Explicit
Sub AddZeroes()
'Declarations
Dim cl As Range
Dim i As Long, endrow As Long
Application.ScreenUpdating = False
'Converts the A column format to Text format
Columns("A:A").NumberFormat = "@"
'finds the bottom most row
endrow = ActiveSheet.Range("A1048576").End(xlUp).Row
'## Or, for Excel 2003 and prior: ##'
'endrow = ActiveSheet.Range("A65536").End(xlUp).Row
'loop to move from cell to cell
For i = 1 To endrow - 1
Set cl = Range("A" & i)
With cl
'The Do-While loop keeps adding zeroes to the front of the cell value until it hits a length of 7
Do While Len(.Value) < 7
.Value = "0" & .Value
Loop
End With
Next i
Application.ScreenUpdating = True
End Sub
选项显式
子AddZeroes()
"宣言",
Dim cl As范围
暗我一样长,尾行一样长
Application.ScreenUpdating=False
'将A列格式转换为文本格式
列(“A:A”).NumberFormat=“@”
'查找最底层的行
endrow=ActiveSheet.Range(“A1048576”).End(xlUp).Row
“###或者,对于Excel 2003及之前的版本:##”
'endrow=ActiveSheet.Range(“A65536”).End(xlUp).Row
'循环以从一个单元格移动到另一个单元格
对于i=1到尾行-1
设置cl=范围(“A”和“i”)
与cl
'Do While循环不断向单元格值的前面添加零,直到其长度达到7为止
当Len(.Value)<7时执行
.Value=“0”和.Value
环
以
接下来我
Application.ScreenUpdating=True
端接头
那么错误是什么呢?我在末尾添加了错误是什么?您是否收到特定的错误消息?发生此错误时,i
的值是多少?这正是导致此错误的原因,非常感谢您的帮助。很高兴它成功了!请注意对代码其余部分的一些修订。尽量避免像Select
和ActiveCell
这样的事情(99.9%的情况下)。:)通常,这些构造完全不是必需的。
Option Explicit
Sub AddZeroes()
'Declarations
Dim cl As Range
Dim i As Long, endrow As Long
Application.ScreenUpdating = False
'Converts the A column format to Text format
Columns("A:A").NumberFormat = "@"
'finds the bottom most row
endrow = ActiveSheet.Range("A1048576").End(xlUp).Row
'## Or, for Excel 2003 and prior: ##'
'endrow = ActiveSheet.Range("A65536").End(xlUp).Row
'loop to move from cell to cell
For i = 1 To endrow - 1
Set cl = Range("A" & i)
With cl
'The Do-While loop keeps adding zeroes to the front of the cell value until it hits a length of 7
Do While Len(.Value) < 7
.Value = "0" & .Value
Loop
End With
Next i
Application.ScreenUpdating = True
End Sub