Excel & VBa: find last row, column, cell in an Excel (work)sheet
Please visit the following site for the original Article.
I found it extremely useful, hence, decided to copy it here for future reference.
User Rating:



/ 7
PoorBest
|
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: 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 Find the last used Column on a Worksheet: Find the last used Cell on a Worksheet:
Private Sub FindLastCell() Dim LastColumn As Integer Dim LastRow As Long Dim LastCell As Range 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 ‘Search for any entry, by searching backwards by Columns. LastColumn = Cells.Find(What:="*", After:=[A1], _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column MsgBox Cells(LastRow, LastColumn).Address End If End Sub Add a row at each change in a column.
Assume you have a long list of data and you want to insert a row at each change. While you could use a simple Loop this method is much faster. The Data must be sorted! Option Explicit
Private Sub InsertRowAtEachChange() ”’ On error goto the error handler defined in the lower part of this function. ”’ Ensure an entire Column is selected ”’ Add a column for formulas ”’ Remove an instance ”’ Exit the Sub End Sub |