我想将查找最后一行代码分配给函数(CdiscChangesReport#create
)
所以我可以随时调用它。我有一个正在运行的代码,但我无法将其分配给一个函数。
以下是我要分配给函数的代码
Private Sub findLastRowFn
这样的事情就是我想要实现的目标。
Dim LastRow As Long, LastCell As Range
' use Find function to get last row
Set LastCell = .Cells.Find(What:="*", After:=.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
searchorder:=xlByRows, searchdirection:=xlPrevious, MatchCase:=False)
If Not LastCell Is Nothing Then
LastRow = LastCell.Row
Else
MsgBox "Error!", vbCritical
End If
答案 0 :(得分:1)
使用以下功能:
Function findLastRowFn(ws As Worksheet) As Long
Dim LastRow As Long, LastCell As Range
' use Find function to get last row
Set LastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, _
searchorder:=xlByRows, searchdirection:=xlPrevious, MatchCase:=False)
If Not LastCell Is Nothing Then
findLastRowFn = LastCell.Row
Else
MsgBox "Error!", vbCritical
End If
End Function
在Sub
中,使用:
LastRow = findLastRowFn(ActiveWorkbook.Sheets("Sheet1"))