如何查找包含特定列和特定工作表中数据的最后一行?
怎么样:
Function GetLastRow(strSheet, strColumn) As Long Dim MyRange As Range Set MyRange = Worksheets(strSheet).Range(strColumn & "1") GetLastRow = Cells(Rows.Count, MyRange.Column).End(xlUp).Row End Function
评论
这个
Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
即使最后一行中只有一个单元格有数据,也会返回最后一个单元格的行号.
你应该使用.End(xlup)
但不是使用65536你可能想要使用:
sheetvar.Rows.Count
这样它适用于Excel 2007,我相信它有超过65536行
function LastRowIndex(byval w as worksheet, byval col as variant) as long dim r as range set r = application.intersect(w.usedrange, w.columns(col)) if not r is nothing then set r = r.cells(r.cells.count) if isempty(r.value) then LastRowIndex = r.end(xlup).row else LastRowIndex = r.row end if end if end function
用法:
? LastRowIndex(ActiveSheet, 5) ? LastRowIndex(ActiveSheet, "AI")
简单快捷:
Dim lastRow as long Range("A1").select lastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row
使用示例:
cells(lastRow,1)="Ultima Linha, Last Row. Youpi!!!!" 'or Range("A" & lastRow).Value = "FIM, THE END"