Option Explicit
Sub Test_xlFirstLastRows()
Dim SheetName As String
MsgBox "Worksheet name = " & ActiveSheet.Name & vbCrLf & _
"First non-blank row = " & xlFirstRow & vbCrLf & _
"Last non-blank row = " & xlLastRow, vbInformation, _
"Active Sheet Demonstration"
SheetName = "Sheet4"
MsgBox "Worksheet name = " & SheetName & vbCrLf & _
"First non-blank row = " & xlFirstRow(SheetName) & vbCrLf & _
"Last non-blank row = " & xlLastRow(SheetName), vbInformation, _
"Passed Sheet Name Demonstration"
End Sub
Function xlFirstRow(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
With Worksheets(WorksheetName)
On Error Resume Next
xlFirstRow = .Cells.Find("*", .Cells(.Cells.Count), xlFormulas, _
xlWhole, xlByRows, xlNext).Row
If Err <> 0 Then xlFirstRow = 0
End With
End Function
Function xlLastRow(Optional WorksheetName As String) As Long
If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
With Worksheets(WorksheetName)
On Error Resume Next
xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _
xlWhole, xlByRows, xlPrevious).Row
If Err <> 0 Then xlLastRow = 0
End With
End Function
|