code_junkie

How can I find last row that contains data in the excel sheet with a macro?

Nov 10th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. Sub GetLastRow(strSheet, strColum)
  2. Dim MyRange As Range
  3. Dim lngLastRow As Long
  4.  
  5. Set MyRange = Worksheets(strSheet).Range(strColum & "1")
  6.  
  7. lngLastRow = Cells(65536, MyRange.Column).End(xlUp).Row
  8. End Sub
  9.  
  10. function LastRowIndex(byval w as worksheet, byval col as variant) as long
  11. dim r as range
  12.  
  13. set r = application.intersect(w.usedrange, w.columns(col))
  14. if not r is nothing then
  15. set r = r.cells(r.cells.count)
  16.  
  17. if isempty(r.value) then
  18. LastRowIndex = r.end(xlup).row
  19. else
  20. LastRowIndex = r.row
  21. end if
  22. end if
  23. end function
  24.  
  25. ? LastRowIndex(ActiveSheet, 5)
  26. ? LastRowIndex(ActiveSheet, "AI")
  27.  
  28. Function LastRow(rng As Range) As Long
  29. Dim iRowN As Long
  30. Dim iRowI As Long
  31. Dim iColN As Integer
  32. Dim iColI As Integer
  33. iRowN = 0
  34. iColN = rng.Columns.count
  35. For iColI = 1 To iColN
  36. iRowI = rng.Columns(iColI).Offset(65536 - rng.Row, 0).End(xlUp).Row
  37. If iRowI > iRowN Then iRowN = iRowI
  38. Next
  39. LastRow = iRowN
  40. End Function
  41.  
  42. Selection.End(xlDown).Select
  43. MsgBox (ActiveCell.Row)
  44.  
  45. Public Function LastData(rCol As Range) As Range
  46.  
  47. Set LastData = rCol.Find("*", rCol.Cells(1), , , , xlPrevious)
Add Comment
Please, Sign In to add comment