• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Opening VBA Editor To A Specific Line Of Code?

Something I've been trying to do for long time now is opening the VBA editor to go in directly to a specific line of code.


I've been using the 3 codes beneath (but my codes have been so lengthy it would really be helpful for me to jump in exactly at a specified line).


Sub Open_VBA_Editor01()

'''' Opens General Editor

Application.VBE.MainWindow.Visible = True

End Sub


Sub Open_VBA_Editor02()

'''' Specific Standard Module

With Application

.Goto "My_Macro_Test"

End With


End Sub


Sub Open_VBA_Editor03()

''' Specific User Form Code

With Application.VBE

.ActiveVBProject.VBComponents("My_UserForm").CodeModule.CodePane.Show

End With


End Sub
 
Basic code:

[pre]
Code:
Sub OpenVBEGoToSpecificMacro()

Dim lStartLine As Long
Application.VBE.MainWindow.Visible = True
ThisWorkbook.VBProject.VBComponents("ModuleNameHere").Activate

With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("VBAProcedureNameHere", 0)
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
Chande the module and macro references as needed. The key part is the SetSelection line. You can use the VB help file, but the 4 arguements are start line, start column, end line, end column. This line:

lStartLine = .ProcStartLine("VBAProcedureNameHere", 0)
[/pre]
Is getting the first line of the specified macro. So, the code is saying to select the 1st line and 1st column through 1st line and 1st column. I'd recommend playing around with it, see how it works.
 
Back
Top