Consulting

Results 1 to 11 of 11

Thread: Scroll to a named macro in the VBE

  1. #1

    Scroll to a named macro in the VBE

    In order to scroll to the macro named myMacro in a VBE Standard Module, I can write code like this:

    [vba]Sub scrollToMyMacro()
    Application.GoTo "myMacro"
    End Sub[/vba]
    I can assign this Sub to a button on any sheet and it will instantly teleport to myMacro. The VBE will be opened if it's not open, and the Standard Module containing myMacro will be brought to the front with the cursor positioned at the start of Line1.

    This works amazingly well for such a simple command, but I can't get it to work if myMacro lives in a Class Module or in an Addin.

    I'd like to be able to jump to a macro that lives in a Userform Module. Is that possible?

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,053
    Location
    Have you tried to identify the object (your userform by name) and then use the Application.GoTo?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Well, I tried, but I couldn't find any syntax that worked. When GoTo is used like this it's pretty restrictive about what it will accept for an argument.

    This is the error I get with that procedure when myMacro is in a Userform module.
    "The text you entered is not a valid reference or a defined name."

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    A userform (or regular class module) does not exist as an object until you load it so the reference is not valid. For a worksheet module you can simply use "sheet_codename.routine_name"
    Be as you wish to seem

  5. #5
    The Userform is loaded, so that's not the reason why the reference is invalid.

    As for "sheet_codename.routine_name", have you actually tried that with GoTo?

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Yes, I have.
    Be as you wish to seem

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    It may be the fact that a userform is not an excel object - why are you trying to jump to code in a running userform instance?
    Be as you wish to seem

  8. #8
    This is a tool for code development. I can trace the program flow through many different modules without having to know which module the procedure lives in. This works extremely well in Standard Modules.

    Could you share the code you used to jump to a procedure in a Worksheet Module?

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Shift+f2 takes you straight to the definition of a routine from its name.

    I simply used:
    [vba]application.goto "sheet1.mysub"[/vba]
    Be as you wish to seem

  10. #10
    Thanks, Aflatoon. Application.GoTo does indeed work for Worksheet Modules when the procedure name is qualified with the sheet's code name, but Shift-f2 still seems to work only for Standard Modules.

    So, is there any way I can achieve the same effect of jumping to a procedure in a Form Module, Class Module or Add-in, possibly even using something other than GoTo?

  11. #11
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Shift+f2 (or right-click, Definition) should work with classes and forms if the highlighted routine name is properly qualified - so for example if you declare a variable as Object then Shift+f2 won't work to jump to the code in its class but if you declare it as Class1 for example, it will work.
    Be as you wish to seem

Posting Permissions

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