abenitez77
Board Regular
- Joined
- Dec 30, 2004
- Messages
- 149
I am using this sub to get the last row and column with data. If it tries to search a column that is empty, it errors out. How can i avoid this ? or do this better?
Code:
Public Sub GetLastRow(RowLast As Long, ColLast As Long) Dim lastRow As Range, ws As Worksheet Dim lastColumn As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
pLastRow = 0
pLastCol = 0
Set ws = ActiveSheet
'Get Last Row with Data in Worksheet using SpecialCells
'Debug.Print ws.Cells.SpecialCells(xlCellTypeLastCell).Row
'Set lastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).EntireRow
Set lastColumn = ws.Cells.SpecialCells(xlCellTypeLastCell).EntireColumn
RowLast = Columns(4).Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious, After:=Cells(18000, 4)).Row
'RowLast = lastRow.Row
ColLast = lastColumn.Column
' pLastRow, pLastCol
If RowLast < 1 Then
pLastRow = 1
Else
pLastRow = RowLast
End If
If ColLast < 1 Then
pLastCol = 1
Else
pLastCol = ColLast
End If
Quit: Application.ScreenUpdating = True Application.EnableEvents = True On Error GoTo -1: On Error GoTo 0
End Sub