How do I get the name of a subroutine (VBA / Excel) within itself to use in a "Trace" type code? ( See basic code in the UPDATE block after the sample code )
The idea is to use something like "ME.Name", which takes the name of the project.
For example, the ideal would look something like:
Private Sub Rotina()
Trace(???.Name) 'Captura o nome da subrotina como argumento do Trace
...
...
End Sub
Public Sub Trace(ByVal NomeDaSubRotina As String)
if TraceAtivo Then
MsgBox "Estou em " & NomeDaSubRotina
End If
'Pode ser feito também com ListBox (ver código na atualização abaixo)
End Sub
REQUEST
Please, I found several solutions to the question, but I do not have sufficient knowledge in VBA to test them. Can anyone point to one of these solutions? Follow the link:
Thankful
COMPLEMENT: Search Result!
Through this "link", I did some tests with the site codes and did the same with other "sites" within this context.
The result that I've come up with in a number of different attempts is that a message always looks something like this:
"It is not safe to use this function"
So, "YOU CAN CAPTURE THE SUBJECT NAME WITHIN THE SAME, BUT YOU MUST HAVE A CERTAIN TYPE OF PERMISSION !!!"
Searching the message, I noticed that there are explanations of how to do this, however, I did not quite understand how to do it. I did not indicate the links at the moment, because I did the searches outside the house and I would have to do the survey where I found, BUT I imagined that I would have time at the moment to take the links and summarize what each one contained, and did not give.
QUESTION:
What are the consequences and / or risks of changing the default level of security and, if you decide to change, how to proceed? I did not understand very well and did not want to risk without knowing it. Does anyone know about this?
Thankful
UPDATE
The code below functions as a "Trace" and dynamically, and it is very interesting to see Listbox present the sequence of subroutines triggered by each action (in block, from the beginning to the end of that action's operations).
It shows by the MsgBox and / or adds lines to the Listbox with the "Name" of the subroutine where "Msg" is (put in the first row of each subroutine).
According to the option: 1, 2 or 3 in a cell in Excel some or all subroutines are shown, because when I give the other option a positive integer (in another cell in Excel), only one or some subroutines indicated with this same number are shown, or all, if the option is -1.
You just need to get the name of each subroutine automatically inside itself so you do not have to type your name inside each one.
Public Sub Msg(ByRef MsgTexto As String, ByRef MsgTipo As Integer)
' Msg - PARAMETROS: Digitar na planilha nas células:
' MsgOuListbox e MsgTipo os valores dos parâmetros.
'
‘ A célula MsgIndice inicia com 0 quando o formulário é acionado,
‘ e guarda o índice para inserir cada nova linha ao Listbox
'
' MsgOuListbox: 0=Não mostra, 1=Mostra MsgBox 2=Preenche ListBox 3=Ambos
'
' MsgTipo: Indica se mostra MsgBox e/ou acrescenta linha ao ListBox de cada
' subrotina conforme o número indicado na chamada dentro de cada rotina:
' 0 = não mostra
' 1 ... n = Faz só para o código indicado (um número apenas, pode ser
' individual de uma subrotina ou um grupo delas com este mesmo número)
' -1 = faz para todas as subrotinas
'
' O listbox foi nomeado como ListBoxMsg1
' Se uma das células tem 0 não faz
If Range("MsgOuListbox").Value = 0 Or Range("MsgTipo").Value = 0 Then Exit Sub
' se tipo 2 ou 3 mostra MsgBox com o nome enviado da rotina
If Range("MsgOuListbox").Value = 2 Or Range("MsgOuListbox").Value = 3 Then
' se o número do tipo enviado é o mesmo do parâmetro ou se escolheu -1,
' acrescenta linha ao Listbox
If MsgTipo = Range("MsgTipo").Value Or Range("MsgTipo").Value = -1 Then
ListBoxMsg1.AddItem MsgTexto, Range("MsgIndice").Value
Range("MsgIndice").Value = Range("MsgIndice").Value + 1
End If
End If
' se tipo 1 ou 3 pode mostrar o nome enviado com MsgBox
If Range("MsgOuListbox").Value = 1 Or Range("MsgOuListbox").Value = 3 Then
' se o número do tipo enviado é o mesmo do parâmetro ou se escolheu -1,
' mostra MsgBox
If MsgTipo = Range("MsgTipo").Value Or Range("MsgTipo").Value = -1 Then
MsgBox MsgTexto
End If
End If
End Sub
Within each subroutine I put in the beginning:
Msg "RoutineName", 1
in another:
Msg "AnotherSubRotina", 12
If you put cells in the MsgOuListbox = 2 and MsgType = 12 worksheets, only the name of the subroutines I gave the 12 number will appear dynamically in the Listbox (if triggered during execution), in this example "OtherRotline" will be shown and "RoutineName" will not be shown .