Results 1 to 5 of 5

Thread: VBA Open Module at specific (variable) procedure

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Rep Power
    0
    That works almost exactly as I want it to! Thank you!

    The only thing it is missing is just opening the code module without going to a specific sub/function where the ActiveCell is outside of the range containing the lists/the Cell Value is blank - at the moment either of these scenarios causes it to error. The latter should be easy enough (put an If Then statement in to check whether ActiveCell.Value is NULL), but the former is a little more troublesome. Is there an easy way of checking whether the ActiveCell is in a specific range?

  2. #2
    Member p45cal's Avatar
    Join Date
    Oct 2013
    Posts
    94
    Rep Power
    12
    Is there only ever going to be one code module? It could be quite restrictive if there were only to be one. Is it always going to have the name Module 1? The recording of a macro will frequently create a new one. I can certainly get the vbe to open at the top of module 1:
    Code:
    Dim VBAEditor As VBIDE.VBE
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    
    Set VBAEditor = Application.VBE
    Set VBProj = VBAEditor.ActiveVBProject
    Set VBComp = VBProj.VBComponents("Module1")
    Set CodeMod = VBComp.CodeModule
    
    VBAEditor.MainWindow.Visible = True
    CodeMod.CodePane.Show
    LineNum = 1
    If Not Intersect(ActiveCell, ActiveSheet.Range("AM23:AM36").SpecialCells(2)) Is Nothing Then LineNum = CodeMod.ProcStartLine(ActiveCell.Value, vbext_pk_Proc)
    CodeMod.CodePane.TopLine = LineNum
    CodeMod.CodePane.SetSelection LineNum, 1, LineNum, 1
    As an aside, I note that the code which populates that section of the sheet has the line:
    Code:
    Rng(1, 2).Value = ProcKindString(ProcKind)
    which tries to put the likes of "Sub or Function" in the cell immediately to the right of where the procedure name is placed, but because the cells are merged nothing gets put there. If you were to unmerge some cells in that vicinity that data could become visible/available, furthermore, you could have an additional cell in the row which contains the name of the module that that procedure is in, for example:
    Code:
    Rng(1, 8).Value = .Name
    which you could later use, either in a loop to run through the code modules, or to allow the single module to have any name by changing the line:
    Code:
    Set VBComp = VBProj.VBComponents("Module1")
    to:
    Code:
    Set VBComp = VBProj.VBComponents(ActiveCell(, 8).Value)
    Last edited by p45cal; 02-24-2014 at 06:34 PM.

Similar Threads

  1. Replies: 5
    Last Post: 12-07-2013, 12:35 PM
  2. Replies: 1
    Last Post: 08-23-2013, 06:33 PM
  3. Manipulate VBA Array Object Using Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 06-06-2013, 07:53 PM
  4. VBA Procedure for nested if statement.
    By _google in forum Excel Help
    Replies: 1
    Last Post: 02-08-2013, 10:16 PM
  5. VBA To Pass A Variable In An Excel Formula
    By devcon in forum Excel Help
    Replies: 4
    Last Post: 12-17-2012, 09:12 PM

Posting Permissions

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