Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 代码执行错误持久化_Vba_Excel - Fatal编程技术网

Vba 代码执行错误持久化

Vba 代码执行错误持久化,vba,excel,Vba,Excel,我已经使用这段代码向IDs添加零有一段时间了,它总是能够正常工作。直到今天,它才开始说代码执行被中断了 Sub AddZeroes() 'Declarations Dim i As Long, j As Long, endrow As Long 'Converts the A column format to Text format Application.ScreenUpdating = False Columns("A:A").Select Selection.NumberFormat =

我已经使用这段代码向IDs添加零有一段时间了,它总是能够正常工作。直到今天,它才开始说代码执行被中断了

Sub AddZeroes()
'Declarations
Dim i As Long, j As Long, 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

这是Excel的一个奇怪的小错误。当您点击CTRL+BREAK停止代码执行时,就会发生这种情况。这就是它应该出现的时候

有时,如果您使用CTRL+BREAK停止执行,它会停留并不断弹出。这就是问题所在。我想,有时候它也会在不使用CTRL+BREAK的情况下发生

尝试按CTRL+BREAK几次,尝试关闭并重新打开工作簿,尝试重新启动计算机

关闭文件,制作副本,使用副本,然后查看是否仍然发生

如果所有其他操作都失败,您可能需要创建一个新工作簿,并复制所有代码/函数。我听过关于这件事的恐怖故事,最后的建议是唯一的解决办法

代码中没有错

编辑 我想也可能是键盘故障。键盘短路或粘滞,Excel从ESC键或break键接收中断命令。也试试另一台电脑

这里有一点关于您从中添加的代码的附加说明

它应该为您提供更好的性能和更清晰的代码。注意:避免。选择并改进循环的逻辑


检查您的单元格是否包含N/A,DIV/0!那么错误消息和数字是什么呢?没有数字,它只是说:代码执行被中断了当它停止时,“i”的值是多少?然后看那一排的问题。太棒了!这个解决方案几乎不需要付出任何努力,而且在解决“幻影断点”问题方面比我遇到的任何其他解决方案都要好,需要更多的人意识到这一点。现在,如果微软能从一开始就阻止这个问题。。。
DO WHILE LEN(ACTIVECELL.VALUE) < 7
LOOP
Sub AddZeroes()
Application.ScreenUpdating = False

    Dim lastRow As Long, cell As Range

    lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    Range("A1:A" & lastRow).NumberFormat = "@"

    For Each cell In Range("A2:A" & lastRow)
        If Len(cell) < 7 Then cell = "0" & cell
    Next i

Application.ScreenUpdating = True
End Sub