如何将 ActiveSheet、ActiveWorkbook 或 ThisWorkbook 设置为 VBA 中可选参数的默认值?

How can ActiveSheet, ActiveWorkbook or ThisWorkbook be set as the default for an optional parameter in VBA?

我正在尝试将可选参数的默认值设置为 Excel 的 Application.ActiveWorkbook 属性。这是我失败的尝试:

尝试的代码

Function SheetExists(ByVal sheetName As String, _
    Optional ByVal targetBook As Workbook = Application.ActiveWorkbook) As Boolean

结果

此代码导致

Constant expression required : compile error

问题

  1. 什么是 "Constant Expression"?
  2. 是否有另一种方法可以使用 ActiveWorkbook 属性(和其他属性)作为函数和子例程中参数的默认值?

你能用这样的东西吗:

Public function SheetExists(ByVal sheetName As String, Optional wb As Workbook)

  If wb Is Nothing Then Set wb =  Application.ActiveWorkbook 'or ThisWorkbook

  'your code here, for example
  wb.Application.Calculation = xlAutomatic 
End Sub