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,
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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
 

dullboy

New Member
Joined
Aug 29, 2002
Messages
22
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
 

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,720
Members
414,401
Latest member
grenona2020

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
Top