Sub Name

ghudson

Registered User.
Local time
Today, 00:17
Joined
Jun 8, 2002
Messages
6,194
Code:
MsgBox Application.VBE.ActiveCodePane.CodeModule
That gives me the name of the module. How can I get the name of the sub or function that the code is run from?
 
I'm not sure if this is what you're looking for, but look into:

MsgBox Application.VBE.ActiveCodePane.CodeModule.ProcOfLine( , )


I really don't understand why you would need to know the name of where the function is run from... just scroll up and look at it. If it's too hard to type, make a string.



Hope this helps,
Modest
 
Last edited:
I just get a blank message box with that.

I am trying to tighten up the code I use for my runtime error messages. I do use a string for the sub or function name. Using a VBA property of some sort would be nice if it is possible to grab the current sub() or function() name.
 
Code:
Sub ListProcedures()

Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim Msg As String
Dim ProcName As String

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("SaveModule").CodeModule
With VBCodeMod
    StartLine = .CountOfDeclarationLines + 1
    Do Until StartLine >= .CountOfLines
        Msg = Msg & .ProcOfLine(StartLine, vbext_pk_Proc) & Chr(13)
        StartLine = StartLine + _
          .ProcCountLines(.ProcOfLine(StartLine, _    
           vbext_pk_Proc), vbext_pk_Proc)
    Loop
End With
MsgBox Msg

End Sub

Eventhough this is for excel, it might help you learn more.
http://www.cpearson.com/excel/vbe.htm


You might also want to check here:
http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=307
 
Last edited:
ghudson said:
I just get a blank message box with that.

I am trying to tighten up the code I use for my runtime error messages. I do use a string for the sub or function name. Using a VBA property of some sort would be nice if it is possible to grab the current sub() or function() name.

The procofline( ) returns the name of the function that the line number belongs to. You must fill in what line number you want the procedure name for. An example is: procofline(5,0) -- the second parameter is a description, you don't need this, so just put 0.

I'm not quite sure how you could get what line of code the program is running at that specific instant (or if this is even possible). This might all depend on if the code is being interpreted or if it is precompiled. I believe it is interpreted, which would give me some sort of hope to retrieve what line the application is working on, but I am still not sure how you would go about getting that number. My guess is that there is possibly a system debug/watch command you might be able to run --- this is purely just imagination, but someone else might know.

So for right now you would have to manually type the line number.


Edit:
I think on error may return a line number and description of the error, so why are you writing your own error message? Especially since VBA debug goes right to the error for you.

-modest
 
Last edited:

Users who are viewing this thread

Back
Top Bottom