Find formula to find last used row in a column

fraz627

Board Regular
Joined
Apr 26, 2014
Messages
99
Office Version
  1. 2010
Platform
  1. Windows
I have a function to find the last used row of a column. It seems to work fine most of the time, however occasionally it get a value error.
here is the function, it is stored in the first module.
VBA Code:
Public Function LastOccupiedRow(rData As Range)
LastOccupiedRow = rData.Find(What:="*", After:=rData.Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End Function
The actual cell has =LastOccupiedRow(A:A)
It usually happens after running a Macro that disables screen updating, hiding/un-hiding rows, for next loops ect
any suggestions
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
What error are you getting? Without testing the main possibility that comes to mind is that the range being searched is empty.
 
Upvote 0
What error are you getting? Without testing the main possibility that comes to mind is that the range being searched is empty.
the formula is in a cell, I get the #Value error. it seems its done on a for next loop.
 
Upvote 0
That shouldn't make any difference. The way that your code is written, it should give you the last occupied row in the range that is not hidden. If the visible cells are all empty then you will get the error.

You could try making it volatile but I don't think that it is going to make any difference here. If you're not already doing so then you may want to use Application.Calculate after your loop code has finished.

VBA Code:
Public Function LastOccupiedRow(rData As Range) As Long
Application.Volatile
LastOccupiedRow = rData.Find(What:="*", After:=rData.Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End Function
 
Upvote 0
Solution
it should give you the last occupied row in the range that is not hidden.
Not quite. If the hidden rows are hidden using filter this is true and it will ignore the hidden rows but if they have their property set to hidden they are still included in the Find results. This is using Lookin:=xlFormulas. Using Lookin:=xlValues it will ignore hidden regardless of how they are hidden.
Since this part of the Find command is sticky and has other consequences, I would suggest making it specific in the Find code.

If you always want the last row in the column(s) not just the last row in rData, then you could try specifying the EntireColumn eg
VBA Code:
Public Function LastOccupiedRow(rData As Range)
    LastOccupiedRow = rData.EntireColumn.Find(What:="*", After:=rData.EntireColumn.Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End Function
 
Last edited:
Upvote 0
Try this.
VBA Code:
Public Function LastOccupiedRow(rData As Range)
If rData.Cells.Count > Evaluate("Countblank(" & rData.Address & ")") Then
LastOccupiedRow = rData.Find(What:="*", After:=rData.Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Else
LastOccupiedRow = ""
End If
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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