Thursday, December 3, 2009

Excel & VBa: find last row, column, cell in an Excel (work)sheet

Source:
http://www.ozgrid.com/VBA/ExcelRanges.htm

The following page contains some usefull (general) vba code that can be used to find the last row, columnand/or cell in an Excel (work)sheet.

Find the last used cell, before a blank in a Column:
Sub LastCellBeforeBlankInColumn()
Range("A1").End(xldown).Select
End Sub

Find the very last used cell in a Column:
Sub LastCellInColumn()
Range("A65536").End(xlup).Select
End Sub

Find the very last used cell in a Column:
Sub LastCellInColumn()
Range("A65536").End(xlup).Select
End Sub

Find the last cell, before a blank in a Row:
Sub LastCellBeforeBlankInRow()
Range("A1").End(xlToRight).Select
End Sub

Find the very last used cell in a Row:
Sub LastCellInRow()
Range("IV1").End(xlToLeft).Select
End Sub

Find the very last used cell on a Worksheet:
Sub Demo()
Cells.Find(What:="*", After:=[A1], SearchDirection:=xlPrevious).Select
End Sub

Find the last used Row on a Worksheet:
Sub FindLastRow()
Dim LastRow As Long

If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
LastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
MsgBox LastRow
End If
End Sub

Find the last used Column on a Worksheet:
Sub FindLastColumn()
Dim LastColumn As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
MsgBox LastColumn
End If
End Sub

==============================================

No comments:

Post a Comment