PDA

View Full Version : [SOLVED] Running specific line of macro



if_necessary
02-19-2009, 04:11 PM
Is there a way to run a specific line of a macro in the VBA editor?

For example, suppose my macro is 200 lines long, and I just want to run the 100th line.

As far as I can tell, Step Into/Step Over requires you to run the previous 99 lines before running the 100th line.

I suppose you could comment out the 99 lines, but then when you want to run the whole macro you have to uncomment them.

Thanks for any reply.

Paul_Hossler
02-19-2009, 04:31 PM
If you want to run up to the 100th line, you could put a breakpoint on the line (F9) and run the procedure (F5). The macro will run, but stop on the line.

Why do you not want to execute the lines before it?



Paul

mdmackillop
02-19-2009, 04:32 PM
Just add a GoTo

Sub test()
GoTo Line100
Line100:
doevents
End Sub

Bob Phillips
02-19-2009, 05:04 PM
Just add a GoTo

Sub test()
GoTo Line100
Line100:
doevents
End Sub

No, no, no!!!!

If there is a reason to start at a particular point, have a contol procedure with an entry id, break it down into modules, and select case to execute the appropriate module.

Whilst I will argue against the 'anti-Goto police', we shouldn't propose goto solutions. In the wrong hands, they can be messy.

mdmackillop
02-19-2009, 05:42 PM
I understood this was for debugging and a temporary measure.

Cosmo
02-20-2009, 06:21 AM
If it's for debugging purposes, I always just drag the arrow for the selected line to the line that I want to go to.

if_necessary
02-20-2009, 07:09 AM
Thanks for all the replies. These are all good ideas.

I have two purposes: debugging, and also occasionally I need a quick operation that I know I can get by running one or more lines in an existing macro.

I agree that the best way to do such things is to create additional macros and copy and paste code, but it's nice to know the quick way since it happens frequently enough.

Paul_Hossler
02-20-2009, 08:08 AM
Another way is to copy the line and paste into the "Immediate WIndow" (control-G) in the editor, and hit enter



Sub aaa()
MsgBox "one"
MsgBox "two"
MsgBox "three"
MsgBox "four"
End Sub


If I copy the 'three' line and paste it into the immediate window, I get the msgbox "three"

Paul

nst1107
02-20-2009, 08:22 AM
If I copy the 'three' line and paste it into the immediate window, I get the msgbox "three"

Sweet! I did not know you could do that.