vba 单步执行 (F8) 代码突然执行所有代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11574783/
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 16:58:22  来源:igfitidea点击:

Stepping Through (F8) Code suddenly executes all code?

excelvbaexcel-vba

提问by rohrl77

A Macro that I wrote (on another machine) is suddenly acting strange on my computer. I wrote the code in the Excel 2010environment and am trying to step through the code in the same.

我(在另一台机器上)写的一个宏突然在我的电脑上表现得很奇怪。我在Excel 2010环境中编写了代码,并试图在相同的环境中逐步执行代码。

Everytime I do so, after a few lines, the rest of the code is immediately executed. If I set a breakpoint it will stop. Sometimes, I can then step through a few more lines before it just auotexecutes again.

每次我这样做时,在几行之后,其余的代码会立即执行。如果我设置断点,它将停止。有时,我可以在它再次自动执行之前单步执行几行。

What gives? I have found no answers on the web... thus my first post on Stackoverflow.

是什么赋予了?我在网上找不到答案……因此我在 Stackoverflow 上发表了第一篇文章。

Thanks for any hints! This is making my life difficult.

感谢您的任何提示!这让我的生活变得困难。

UPDATE: I have now gone back to the original programming environment and the stepping through the code there is no problem. So it must be something in my Excel settings! I can't figure out what it could possible be though.

更新:我现在已经回到原来的编程环境,单步执行代码没有问题。所以它一定是我的 Excel 设置中的东西!我无法弄清楚它可能是什么。

采纳答案by Englehas

I found this solutionalso referenced here. The solution is to make a change to the registry (taken from the source):

我发现这里也引用这个解决方案。解决方案是对注册表进行更改(取自来源):

The registry change affects RPC Debugging, and you can read more about it on the Microsoft website: Debugging COM Clients and Servers Using RPC Debugging

注册表更改会影响 RPC 调试,您可以在 Microsoft 网站上阅读更多相关信息:使用 RPC 调试调试 COM 客户端和服务器

1.Close Excel

1.关闭Excel

2.Make a backup of the Registry file, then open the Registry -- there are instructions on the Microsoft website

2.备份Registry文件,然后打开Registry——微软官网上有说明

3.Go to the applicable Registry Key:

3.转到适用的注册表项:

  • For 32-bit Office on 64-bit Window go to registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VBA
  • For 32-bit Office on 32-bit Window go to registry key: KEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\VBA
  • For 64-bit Office on 64-bit Windows go to registry key: KEY_LOCAL_MACHINE\SOFTWARE\ Microsoft\VBA
  • 对于 64 位窗口上的 32 位 Office,请转到注册表项:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VBA
  • 对于 32 位窗口上的 32 位 Office,请转到注册表项:KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VBA
  • 对于 64 位 Windows 上的 64 位 Office,请转到注册表项:KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VBA

4.Right-click in the window at the right, and click New

4.在右侧窗口右击,点击新建

5.Click DWORD (In the screen shot below, the DWORD is for 32-bit Office running on a 64-bit computer)

5.单击 DWORD(在下面的屏幕截图中,DWORD 用于在 64 位计算机上运行的 32 位 Office)

6.Name the DWORD as DisableOrpcDebugging7

6.将 DWORD 命名为 DisableOrpcDebugging7

7.Right-click the DWORD, and click Modify

7.右键单击DWORD,然后单击修改

8.Change Value to 1, and click OK.

8.将值更改为 1,然后单击确定。

9.The completed DWORD will appear in the Registry

9.完成的DWORD将出现在注册表中

10.Close the Registry, and re-open Excel, where the F8 key should now work correctly, stepping through the code.

10.关闭注册表,然后重新打开 Excel,此时 F8 键应该可以正常工作,逐步执行代码。

回答by Trace

Since you mentioned that the F8 button works as expected at first, most of my thoughts are cleaned from the table.
I cannot explain this, but perhaps I can provide a temporary "workaround", which is the best I can do.
You can simulate F8 by clicking each line followed by applying CTRL+F8 (Run to cursor). The cursor will act as a breakpoint and it is less bothersome than placing / removing breaks on each and every line.
It is a little bit more handwork, but it goes as fast (at least on a regular desktop). Hope it can at least reduce some of your frustration when debugging!

既然你一开始提到 F8 按钮按预期工作,我的大部分想法都从桌子上清除了。
我无法解释这一点,但也许我可以提供一个临时的“解决方法”,这是我能做的最好的。
您可以通过单击每一行然后应用 CTRL+F8(运行到光标)来模拟 F8 。光标将充当断点,它比在每一行上放置/删除断点更麻烦。
它需要更多的手工操作,但它运行得同样快(至少在普通桌面上)。希望它至少可以减少您在调试时的一些挫败感!

回答by Christian Geiselmann

I had the same issue, however in Word (not Excel) 2010.

我遇到了同样的问题,但是在 Word(不是 Excel)2010 中。

I tried the various things mentioned by other contributors above, e.g. closing other programmes that might utilise the F8 key for something, and closing other MS Office 2010 applications that might interfere, however, the issue was stubbornly there: from a certain point of my VBA code, the step-by-step execution of the code (F8 key) was ignored, and the entire procedure was executed at once.

我尝试了上面其他贡献者提到的各种事情,例如关闭其他可能使用 F8 键的程序,并关闭其他可能会干扰的 MS Office 2010 应用程序,但是,问题顽固地存在:从我的 VBA 的某个角度来看代码,忽略了代码的分步执行(F8键),一次执行了整个程序。

I then made a number of experiments with my code. Thus I found: in my case, the unwanted behaviour appeared right when I called an external procedure as follows:

然后我用我的代码做了一些实验。因此我发现:在我的情况下,当我调用外部程序时,出现了不需要的行为,如下所示:

Application.Run MacroName:="OneProcedureOfMine"

To circumvent the issue, I rewrote the code just like so:

为了规避这个问题,我像这样重写了代码:

Call OneProcedureOfMine

which is anyway the more elegant way. And it solved the problem. With the subordinate procedures called that way, F8 steps neatly through the main procedure, and each procedure called from within it.

无论如何,这是更优雅的方式。它解决了这个问题。以这种方式调用从属程序后,F8 会整齐地逐步执行主程序,并从其中调用每个程序。

回答by rohrl77

FINAL UPDATE: It turns out that another application which I had running the background, called KeyRocket, and which is designed to help you memorize keyboard shortcuts in office applications, was responsible for the screwy behavior. I hope this helps someone else!

最终更新:事实证明,我在后台运行的另一个应用程序称为 KeyRocket,旨在帮助您记住办公应用程序中的键盘快捷键,导致了这种奇怪的行为。我希望这对其他人有帮助!

And finally thanks to everyone for their suggestions!

最后感谢大家的建议!

回答by HattrickNZ

I had a similar issue, but closed excel down and saw there was a EXCEL.EXE *32process still running, killed that, then reopened and ran macro again. could not reporduce and macro stepped though as expected.

我有一个类似的问题,但关闭 excel 并看到有一个EXCEL.EXE *32进程仍在运行,将其杀死,然后重新打开并再次运行宏。无法按预期进行报告,宏观步调一致。

回答by Marty Wichter

I have successfully fixed these problems by doing two things: 1. If you have more than one Keyboard driver loaded, removed the one you are not using 2. If your keyboard has a "F Lock" key (located to the right of F12 on my Keyboard), press it and try again.

我通过做两件事成功地解决了这些问题: 1. 如果您加载了多个键盘驱动程序,请删除您不使用的驱动程序 2. 如果您的键盘有一个“F Lock”键(位于 F12 右侧)我的键盘),按下它并重试。

回答by C Suttle

I know this is a very old thread, but I just found it. I check the task manager and found that I had a "stuck" Access process running. I got rid of that and everything works find now.

我知道这是一个非常古老的线程,但我刚刚找到它。我检查了任务管理器,发现我有一个“卡住”的 Access 进程正在运行。我摆脱了它,现在一切正常。