View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
David Welch[_2_] David Welch[_2_] is offline
external usenet poster
 
Posts: 21
Default Debug.Print "Test " vbCodeLineNumber & ": " & varName

TommySzalapski wrote:
Is there a way to get debug.print to print the current line number of
the code? (similar to Debug.Print "Test " vbCodeLineNumber & ": " &
varName)

thanks


I you enable line numbers in a function and then raise an error from
that function, you can get the line number, so:

Option Explicit
Private Const LOG_VARIABLE As Long = vbObjectError + 1

Sub LogVariable(varName As String, varValue As String)
Dim vval
vval = Split(varName, "::")
Debug.Print vval(0) & "(" & vval(2) & ") " & vval(1) & " = " & varValue
End Sub

Sub Test()
10 On Error GoTo Failed
15 Dim MyVar As String
20 MyVar = "Variable has been set"
30 Err.Raise LOG_VARIABLE, "Test::MyVar", MyVar
40 Exit Sub
45 Failed:
50 If Err.Number = LOG_VARIABLE Then
60 LogVariable Err.Source & "::" & Erl, Err.Description
70 Resume Next
80 End If
90 Err.Raise Err.Number, Err.Source, Err.Description
End Sub