Reputation: 960
I couldn't find this asked anywhere. In Visual Basic (excel), I can hit F8 and cycle through each line. But lets say I want to begin the sub procedure, and then after executing the first 2 lines, I'd like to skip to line 200. Until now, I've always just dragged the yellow arrow to the desired line. This is really time consuming and I was wondering if there's any command to simply say "run current line where selected" or something.
Additionally, even if I could begin to run through line by line, and quickly move the yellow selected arrow to the desired line, that would also work.
Upvotes: 3
Views: 36215
Reputation: 3634
I found adding a module for testing and copy pasting snippets of code in it as a best way for troubleshooting.
Upvotes: 0
Reputation: 4550
Unfortunately it is not possible to do what you ask directly.
However, you may comment out the lines of code above the code you want to be executed for example:
Sub Workbook_Open()
'Application.DisplayFullScreen = True
'Application.DisplayFormulaBar = False
'ActiveWindow.DisplayWorkbookTabs = False
''ActiveWindow.DisplayHeadings = False
Application.EnableEvents = True
Password = "1234"
ActiveWorkbook.Protect
ThisWorkbook.Protect (Password = "1234")
End Sub
You may use GoTo
s, but however this is not considered good practice and may actively harm your code:
Sub Workbook_Open()
GoTo ExecuteCode
Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.DisplayHeadings = False
Application.EnableEvents = True
ExecuteCode:
Password = "1234"
ActiveWorkbook.Protect
ThisWorkbook.Protect (Password = "1234")
End Sub
Upvotes: 2
Reputation: 95
This is how I do it - basically if I know that my code up to line 200 is working properly but I'm pretty sure there's an error between 200-300 then before compiling - scroll down to line 200 and mark it (to the left of the code). Then compile it - click F5 and it will execute everything up to line 200 - then you can step through each line thereafter individually.
Upvotes: 1
Reputation: 10408
Right click on the line you want to jump to. Hit the Set Next Statement
option in the menu. That's equivalent to dragging the arrow to that line. (Ctrl-F9 is the hotkey for this action.)
If you want it to quickly execute every line up to a certain line, set a breakpoint then hit run instead of stepping through the code line by line. Do this by clicking on the gray bar to the left side where the yellow arrow appears. A dark red dot should appear and the line should be highlighted in dark red. This tells visual basic to stop when it hits that line.
You can also comment lines out by starting them with an apostrophy.
Finally, you can break code into subroutines and execute them independently of eachother.
Sub Subroutine1()
'This is a commented out line. It does nothing.
MsgBox "Do stuff here"
End Sub
Sub Subroutine2()
Subroutine1 'This will run all the code in subroutine 1
MsgBox "Do more stuff here"
End Sub
In the above example, if you run Subroutine1 you'll get one message box popping up. If you run Subroutine2 you'll get two message boxes.
Upvotes: 6
Reputation: 71217
If you have a 200-liner procedure that does so many things you'd like to skip most of it, it looks like you need to refactor a bit.
Extract "things the procedure is doing" into their own Sub
procedures and Function
scopes. If you have banner-like comments that say things like '*** do something ***
then that's a chunk to extract into its own procedure already.
Stepping through that procedure could then involve stepping over (Shift+F8) the smaller procedures that do one thing, or break and skip the call altogether.
Upvotes: 7
Reputation: 5151
I normally comment out lines of code that I don't want to run with apostrophes. Alternatively, you can break up your code into smaller procedures so that you can easily pick and choose what you want to test/run.
Upvotes: 0