vba:查找最后一行失败,错误91 [英] vba: find last row fails with error 91

查看:311
本文介绍了vba:查找最后一行失败,错误91的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 http:// c厘米/ lastRow(sheet As Worksheet)As Long

lastRow = sheet.Columns(1).Find(*,,,,xlByColumns,xlPrevious).Row

结束函数

如果工作表中包含某些东西,它可以工作。如果它是空的或内容失败,错误91(对象变量或块变量未设置)。



为什么?

解决方案

考虑:

  function lastRow(sheet As Worksheet)As变量
Dim r As Range,lr As Long
设置r = sheet.Columns(1).Find(*,,,,xlByColumns,xlPrevious)
如果r是Nothing Then
lastRow =列中没有
Else
lastRow = r.Row
如果
结束函数


I am using this code from 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

It works if the sheet contains something. If it is empty or has had content it fails with error 91 (object variable or with block variable not set).

Why ?

解决方案

Consider:

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

这篇关于vba:查找最后一行失败,错误91的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆