Search notes:

VBA: reporting the line on which an error occurred with erl()

If a sub or function is created with line numbers, then the erl() function can be used to determine on which line the error occurred.
The following example runs into a division by zero on line 11 which is trapped because of the VBA - language: on error goto[on error goto err_. There, erl reports line 11 as the causing line.
option explicit

sub main() ' {
  1
  2   on error goto err_
  3
  4   debug.print "in main"
  5   debug.print "This sub has line numbers"
  6   debug.print "which make it possible to"
  7   debug.print "report the line when an"
  8   debug.print "error occurs."
  9
 10   dim i as long
 11   i = 42 / 0          ' Division by Zero!
 12
 13   debug.print "This line is not reached"
 14   debug.print "because the assignment to"
 15   debug.print "the varialbe i causes an error."
 16
 17   exit sub
 18
 19   err_:
 20     debug.print "*** Error ***"
 21     debug.print "   " & err.description & " on line " & erl
 22
end sub ' }
Github repository about-VBA, path: /language/error_handling/erl.bas

See also

error handling

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758201191, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/VBA/language/error_handling/erl(69): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78