问题
I have a sheet calls "Recap" and I want to know how much line that I have in this sheet.I tried with this code:
Function FindingLastRow(Mysheet As String) As Long
Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.Worksheets(Mysheet)
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
FindingLastRow = LastRow
End Function
......
in my macro i tried this:
....
Dim lastR As Long
lastR=FindingLastRow("Recap")
msgBox lastR
.....
回答1:
You are passing a string into your FindingLastRow function but are not using it. shiit is the parameter being passed in but you later try to use something called Mysheet.
Function FindingLastRow(Optional MySheet As String, Optional sCOL as String = "A") As Long
Dim sht As Worksheet
Dim LastRow As Long
If Not CBool(Len(MySheet)) Then MySheet = ActiveSheet.Name
Set sht = ThisWorkbook.Worksheets(MySheet)
LastRow = sht.Cells(sht.Rows.Count, sCOL).End(xlUp).Row
FindingLastRow = LastRow
Set sht = nothing
End Function
Sub test_FindingLastRow()
Dim lastR As Long
lastR = FindingLastRow
MsgBox lastR
lastR = FindingLastRow("Recap")
MsgBox lastR
lastR = FindingLastRow("Recap", "B")
MsgBox lastR
End Sub
If no worksheet name is passed in then the currently ActiveSheet's name is used. If no alphabetic column name is passed in it will use column A.
回答2:
If you want to find the last cell in a book other than ThisWorkbook:
Function FindingLastRow( _
ByVal shtName As String, _
Optional ByVal colLetter As Variant, _
Optional ByRef wkBk As Variant _
) As Long
Dim colId As String
If IsMissing(colLetter) Then
colId = "A"
Else
colId = colLetter
End If
Dim myTargBk As Excel.Workbook
If IsMissing(wkBk) Then
Set myTargBk = ThisWorkbook
Else
Set myTargBk = wkBk
End If
Dim sht As Worksheet
Set sht = myTargBk.Worksheets(shtName)
With sht
FindingLastRow = .Cells(.Rows.Count, colId).End(Excel.xlUp).Row
End With
End Function
Used like this:
Sub findLast()
MsgBox FindingLastRow("Sheet1")
End Sub
Or to find the last row of column A in a different open workbook...
Sub findLast2()
Dim w As Excel.Workbook
Set w = Excel.Workbooks("Norf.xlsx")
MsgBox FindingLastRow("Sheet1", , w)
End Sub
Or to find the last row of column B in a different open workbook...
Sub findLast3()
Dim w As Excel.Workbook
Set w = Excel.Workbooks("Norf.xlsx")
MsgBox FindingLastRow("Sheet1", "B", w)
End Sub
回答3:
You're also assuming that the last cell will be in column A which may not be the case, so I tend to revert to something like this:
Function FindingLastRow(sheetName As String) As Long
FindingLastRow = Sheets("sheetName").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
End Function
回答4:
The UsedRange on a Worksheet variable is very helpful here. You really don't need a UDF to get the row count.
LastRow = Worksheets("Recap").UsedRange.Rows.Count
This method only works if your data starts in row 1 and the sheet does not have formatting outside of the data. You could add in the starting row + UsedRange.Cells(1,1).Row if you know the data starts somewhere other than row 1. The second issue prevents the use of UsedRange.
来源:https://stackoverflow.com/questions/29653616/how-to-know-the-last-row-filled-in-vba-excel