vba:查找最后一行失败,错误91

我正在使用这个代码从http://ccm.net/faq/14494-vba-last-non-empty-row-all-versions

Function lastRow(sheet As Worksheet) As Long lastRow = sheet.Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row End Function 

它工作,如果表包含的东西。 如果它是空的或有内容失败,错误91(对象variables或块variables未设置)。

为什么?

考虑:

 Function lastRow(sheet As Worksheet) As Variant Dim r As Range, lr As Long Set r = sheet.Columns(1).Find("*", , , , xlByColumns, xlPrevious) If r Is Nothing Then lastRow = "Nothing in column" Else lastRow = r.Row End If End Function