VBA 代码仅在调试模式下工作,在正常模式下运行时挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3592881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:02:43  来源:igfitidea点击:

VBA code works only in the debugging mode and hangs when running in normal mode

excel-vbavbaexcel

提问by guest1

This problem is driving me crazy. I have the following code:

这个问题让我发疯。我有以下代码:

'unprotect sheet
If.Range("Start").Row+1<.Range("End").Row then
  .Rows(.Range("Start").Row+1 & ":" & .Range("End").Row-1).Select
  Selection.Delete Shift:=xlUp
  'protect sheet
End if

when I run it in debugging mode and trace the code, it works perfectly. But when run the code in a normal mode (not debugging) it gives me an error message as " select method of Range class failed" This errors happens in the line: .Rows(.Range("Start").Row +1 ....that is just after the IF statement. Any ideas? Please help.

当我在调试模式下运行它并跟踪代码时,它运行良好。但是当以正常模式(非调试)运行代码时,它给我一条错误消息“Range 类的选择方法失败”这个错误发生在行中:.Rows(.Range("Start").Row +1 ....就在 IF 语句之后。有任何想法吗?请帮忙。

回答by GSerg

This error usually means you are trying to select a range that belongs to a non-active sheet.

此错误通常意味着您正在尝试选择属于非活动工作表的范围。

You almostalways don't need to select anything:

几乎总是不需要选择任何东西

.Rows(.Range("Start").Row+1 & ":" & .Range("End").Row-1).Delete Shift:=xlUp