考虑以下 VBA 函数:
Function getFirstColumn(Optional sheetName As String) As Long
'In particular the IF statement below.
If sheetName = "" Or sheetName = Null Then sheetName = ActiveWorkbook.ActiveSheet.Name
With ActiveWorkbook.Worksheets(sheetName)
getFirstColumn = .Cells.Find("*", .Cells(1), xlFormulas, xlWhole, xlByColumns, xlNext).Column
If Err <> 0 Then getFirstColumn = 0
End With
End Function
使用此功能时,从第二张打开的工作表中使用它时没有问题。
但是,当代码更改为基于函数的等效代码时,'If IsEmpty(sheetName) Or IsNull(sheetName) Then sheetName = ActiveWorkbook.ActiveSheet.Name
如果我尝试使用不拥有它的工作表中的函数,我会遇到问题:
Erro em tempo de execução '9':
Subscrito fora do intervalo。
它们之间有什么区别?为什么会准确触发这样的错误?