Consulting

Results 1 to 9 of 9

Thread: Running specific line of macro

  1. #1

    Running specific line of macro

    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.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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
    Last edited by Paul_Hossler; 02-19-2009 at 04:45 PM.

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Just add a GoTo
    Sub test()
    GoTo Line100
    Line100:
    doevents
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I understood this was for debugging and a temporary measure.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    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.

  7. #7
    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.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    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

  9. #9
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Quote Originally Posted by Paul_Hossler
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •