如果工作表中存在数据,则返回最后一行 [英] Return last row if there exists data in the sheet
本文介绍了如果工作表中存在数据,则返回最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个函数,该函数将检查现有的工作表是新的还是有数据.如果包含数据,则应返回最后一行,否则必须返回第一行.我正在使用以下代码:
I am writing a function which will check if the already existing sheet is new or has data. If it consists data then it should return the last row, else it must return the first row. I am using the following code:
Private Function GetLastRow(sheetName As String) As Integer
Dim lastRow As Integer
lastRow = CurrentWorkbook.Sheets(sheetName).Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).End(xlUp).Row
GetLastRow = lastRow
End Function
但是在调试时,我收到一条错误消息,提示未设置任何对象.我的代码有任何错误吗?
But on debugging, I get an error that says that there is no object set. Is there any error in my code?
推荐答案
类似
Option Explicit
Public Sub TEST()
Debug.Print GetLastRow(ActiveSheet.Name)
End Sub
Private Function GetLastRow(ByVal sheetName As String) As Long
Dim lastRow As Long
With ActiveWorkbook.Sheets(sheetName)
On Error GoTo returnVal
lastRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End With
GetLastRow = lastRow
Exit Function
returnVal:
GetLastRow = 1
End Function
这篇关于如果工作表中存在数据,则返回最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文