Search notes:

VBA - language: keyword optional

Optional parameters allow to define default values for sub/function parameters that need not be specified when the sub/function is called.
' runVBAFilesInOffice.vbs -word %CD%\keyword-optional -c main

sub main() 

  call xyz("one", "two", "three") 
  call xyz("apple", "bananana") 
  call xyz("Berlin", param_3 := "Paris") 

  activeDocument.saved = true 

 end sub 
 

 private sub xyz (                         _ 
                param_1 as string        , _ 
       optional param_2 as string = "foo", _ 
       optional param_3 as string = "bar"  _ 
 ) 

     selection.typeText param_1 & " - " & param_2 & " - " & param_3 & chr(13) 

 end sub 
Github repository about-VBA, path: /language/subs-and-functions/optional-parameters/keyword-optional.bas
Note that optional parameters must have a constant default value. It's not allowed to define an optional parameter that takes the return value of a function like now (the corresponding compilation error is Constant expression required).
For truly optional parameters (that is: optional parameters without specifying a default value), isMissing() can be used to check if an actual value was passed for the argument.

See also

VBA language

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...', 1737520626, '3.143.7.112', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/VBA/language/subs-and-functions/optional-parameters/keyword-optional(68): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78