我使用的代码来自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(对象变量或未设置块变量).
为什么?
考虑:
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