简体   繁体   English

Excel .delete&.clearcontents stop sub

[英]Excel .delete & .clearcontents stop sub

I had a weird problem: lines like 我有一个奇怪的问题:像

rows("1:1000000").delete

and

rows("1:1000000").clearcontents

stop executed and exited the subs. 停止执行并退出潜艇。
There was no error messages and error handling did not get any error number. 没有错误消息,错误处理没有得到任何错误号。
In debug mode the line got executed and then all and the next thing was just no macro running, exactly like the line meant "end sub". 在调试模式下,行已执行,然后所有和下一件事只是没有宏运行,就像行意味着“结束子”。
I googled it, found nothing. 我用Google搜索,一无所获。
Tried this macro on other computer all to the same result. 在其他计算机上尝试这个宏都得到了相同的结果。
There is no protected cells, no spreadsheet and workbook events. 没有受保护的单元格,没有电子表格和工作簿事件。
Eventually i deleted all the modules in the files and all the subs in the same module and the code run through. 最终我删除了文件中的所有模块以及同一模块中的所有子模块并且代码运行完毕。 Then closed the file without saving, re-opened it and everything worked fine. 然后关闭文件而不保存,重新打开它,一切正常。
My guess is there was something that switched back to normal somewhere in Excel guts. 我的猜测是有一些东西在Excel内部的某个地方恢复正常。
Any idea what it could be? 知道它可能是什么? I am afraid the problem may come back sometime. 我担心问题可能会在某个时候回来。

UPDATE: the problem reappeared again and disappeared after running another macro in another file 更新:问题再次出现并在另一个文件中运行另一个宏后消失

UPDATE2: full code of the sub is here: UPDATE2:sub的完整代码在这里:

Sub GenRep()
Application.EnableEvents = True
Sheets("rep").Activate
On Error Resume Next
Rows("3:1000000").Delete 'this line breaks execution, no further lines are executed, no error code nor warning messages
If Err.Number <> 0 Then Debug.Print "Err: " & Err.Number
NLines% = Sheets("db").Range("a1000000").End(xlUp).Row
Rows("2:" & NLines).FillDown
End Sub

I commented the line which breaks execution 我评论了打破执行的那一行

Try: 尝试:

Sheet1.Range(Rows(1), Rows(1000000)).EntireRow.Delete
Sheet1.Range(Rows(1), Rows(1000000)).ClearContents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM