Last Row: Subscript out of Range error

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi,

Have function to find last row on a specified column and sheet but decided to use code from Ron De Bruin aka:
Rich (BB code):
Public Function LastRow(ByRef wks As Worksheet, Optional ByRef xCol As Long = 1) As Long
    
    With wks
        '#1  LastRow = .Cells(.Rows.Count, xCol).End(xlUp).Row
        '#2  LastRow = .Cells.Find("*", wks.Cells(1, 1), xlPart, xlFormulas, xlByRows, xlPrevious).Row
    End With

    With wks
        '#3 
        LastRow = .Cells.Find(What:="*", After:=.Cells(1, 1), Lookat:=xlPart, LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
    End With


End Function
Commented out line #1 is what I initially used
Commented out line #2 errors with run-time error 9, subscript out of range
Line under #3 works but it's not clear why - the only difference I can see is not using the named argument parameters in #3 vs #2

Could someone explain why this is?

Thanks in advance,
JDI
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
in #2 you have the xlPart & xlFormulas the wrong way round.
When using the parameter names (as in #3 ) you can put the parameters in whatever oder you like.
But when you omit the names you have to put parameters in the correct order. What,After,Lookin, Lookat, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat
 
Upvote 0
Understand, I must have copied it from the link wrong to mix up the parameters like that. Makes sense when name referencing, doesn't require specific order. Thanks @Fluff
 
Upvote 0

Forum statistics

Threads
1,226,739
Messages
6,192,739
Members
453,754
Latest member
milestogo

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