如何在VBA编辑器中跳转到行号?
我在Office 2010中使用VBA.在顶部,有一个带有行号和列号的框,例如:
I'm using VBA in Office 2010. On the top, there's a box with the line number and column number, e.g.:
Ln 1480, Col 17
是否有一种方法可以直接在代码编辑中跳转到另一个行号(而不是在执行中),就像在记事本中使用 Ctrl + G
一样?
Is there a way to jump directly to another line number in code editing (not in execution), the same way that I would use Ctrl+G
in Notepad? This MSDN answer suggests that it's not possible, but I'm hoping that someone has found a way to do this kind of editor navigation.
我知道可以在下拉列表中单击一个过程名称,但是不幸的是,我正在处理一些几百行长的过程,并且在我将其重构之前,能够包含进来非常好在我的错误跟踪器中输入行号,并在解决问题时跳转到该行.
I know that it's possible to just click on a procedure name in the dropdown, but unfortunately I'm working with some procedures that are several hundred lines long and until I get them refactored, it would be great to be able to include a line number in my bug tracker and jump to that line when I'm resolving the issue.
为VBA IDE制作自己的JumpToLine过程
创建一个名为 mdlJumpToLine
的新模块,并添加以下方法:
Create a new module called mdlJumpToLine
and add the following method:
Public Sub JumpToLine(line As Long)
Application.VBE.ActiveCodePane.SetSelection line, 1, line, 1
End Sub
例如,如果要跳转到代码模块或您要在当前代码窗格中打开的类的第1,234行,请在代码模块或类中输入 JumpToLine 1234
立即窗口,然后按Enter.如果该行已经在视图中,则不会执行任何操作,但是如果不在屏幕上,它将自动滚动到屏幕中心
As an example, if you want to jump to a line 1,234 in the code module or class you have open in the current code pane, type JumpToLine 1234
in the immediate window and hit enter. If that line is already in view, this does nothing, but if it's off the screen, it will automatically be scrolled to the center of the screen
信任对VBA项目对象模型的访问权限
如果收到此错误,对象'_Application'的方法'VBE'失败",则必须以编程方式访问受信任的VBE.您可以通过以下操作(在Excel 2007中)转到excel主窗口(而不是VBA IDE)并单击文件" --->选项" --->信任中心" --->信任中心设置"-->宏设置",然后选中信任对VBA项目对象模型的访问"复选框.从那时起,JumpToLine方法应该起作用.
If you get this error, "Method 'VBE' of object '_Application' failed", you will have to make programmatic access to the VBE trusted. You can do this by (in Excel 2007) going to the main excel window (not the VBA IDE) and clicking "File" ---> "Options" ---> "Trust Center" ---> "Trust Center Settings" ---> "Macro Settings" and selecting the checkbox for "Trust access to the VBA project object model". From then on, the JumpToLine method should work.