Finding Row number of last active cell

dullboy

New Member
Joined
Aug 29, 2002
Messages
22
Hi,

I need to find the last row that contains data from within my macro.

I tried the rows.count - but this returns 65000, the max rows of a sheet.

Cheers,
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi dullboy,

Try the following:

<pre>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</pre>

Taken from this site: http://www.ozgrid.com/

HTH
 
Upvote 0
That works great but is there some way I can pass the row number into a variable defined in the sub where it was called from - something like :-

numRows = FindLastRow

Also I would like to be able to pass in a value so it could be used for a different woorkbook. Something like :-

numRows = FindLastRow(1)

then in the sub :-

sub FindLastRow(WBnum)
If Workbooks(WBnum).WorksheetFunction.CountA(Cells) > 0 Then

Cheers,
DB
 
Upvote 0
Hi DB,

You need to make the FindLastRow sub into a function. I've knocked one together and done a quick test - have a go and change to suit.

<pre>Function FindLastRow(wbkFullPath As String, wksName As String) As Long

On Error GoTo NoWBK
Workbooks.Open (wbkFullPath)
On Error GoTo 0

On Error GoTo NoWKS
With ActiveWorkbook.Worksheets(wksName)
On Error GoTo 0
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Rows.
FindLastRow = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
End If
End With

Exit Function
NoWBK:
MsgBox "Workbook not found!"
Exit Function
NoWKS:
MsgBox "Worksheet not found!"
Exit Function

End Function

Sub Test()
Dim LastRow As Long, wbkFullPath As String, wksName As String

wbkFullPath = "C:My DocumentsSpreadsheetsEntDate Save Test.xls"
wksName = "Sheet1"

LastRow = FindLastRow(wbkFullPath, wksName)
MsgBox LastRow

End Sub</pre>
HTH
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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