Get Last Row errors in empty column

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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You would need to either use an If...Then statement to qualify the range you are interrogating for a string, or use a diffenent syntax to get the last row like"
Code:
RowLast = ws.Cells(Rows.Count, 4).End(xlUp).Row

The Find syntax is more useful with evauating the last row for multiple columns.
 
Last edited:
Upvote 0
These two code lines will return the last row number and last column number that contains data...

LastUsedRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row

LastUsedCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
 
Upvote 0
These two code lines will return the last row number and last column number that contains data...

LastUsedRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row

LastUsedCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column

The OP problem was getting the Obect Variable not Set because the column is blank. These only work if there is data or formulas.
 
Last edited:
Upvote 0
The OP problem was getting the Obect Variable not Set because the column is blank.
:confused: Sorry, but I do not understand this. The OP's opening line in the first message was "I am using this sub to get the last row and column with data." Are you saying that is not what he actually wants to do?
 
Upvote 0
:confused: Sorry, but I do not understand this. The OP's opening line in the first message was "I am using this sub to get the last row and column with data." Are you saying that is not what he actually wants to do?

I was going by this statement.
If it tries to search a column that is empty, it errors out. How can i avoid this ?

In the code that was in the OP, the Find method was being used but only for a single column. So I assumed that the error was based on a single column query.
 
Last edited:
Upvote 0
These two code lines will return the last row number and last column number that contains data...

LastUsedRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row

LastUsedCol = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column

This is what I needed. The last row and last column where always different. When the sheet had an empty column it was searching on, it would error out on. Your solution works.

Thanks!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,794
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top