Find Last row number for Col matching Pattern

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,072
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I want to use this is a Function and get the last row number where the first 4 chars in column "Type" are "1990". Format is General
I'm trying this but get subscript out of range
= ThisWorkbook.Sheets("Sheet1").Range("Type").Find(what:="1990*", LookAt:=xlPart, lookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

Thanks for any correction.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
That error message is usually generated when you're trying to reference a sheet tab that doesn't exist. Double check what tab the named range "Type" is on, change Sheet1 to suit and try again.
 
Upvote 0
Sheet Name and Column name are confirmed.
Changing to xlWhole alters the error to Run-time error '1004': Application-defined or object-defined error
But oddly, changing back to xlPart keeps the same error.
 
Upvote 0
Make sure the named range "Type" exists. If it doesn't - create it and if does make sure it's not in error.
 
Upvote 0
"Type" is the name of the Column B - the text in Row 1. Is that the "named range" ?
Or should it be Set Type = Range("B1:B100")
Although 1-100 is a guess, ideally it'd be every row in the worksheet.
 
Upvote 0
In your code...

VBA Code:
ThisWorkbook.Sheets("Sheet1").Range("Type").Find(what:="1990*", LookAt:=xlPart, lookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

...Type is expected to be a named range which are set via the following path Formulas > Define Name. I think you just need this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long

    lngLastRow = ThisWorkbook.Sheets("Sheet1").Range("B1:B100").Find(what:="1990*", LookAt:=xlWhole, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

End Sub

Regards,

Robert
 
Upvote 0
Solution
Thanks Robert, that's nailed it. Range definition was the problem. I found Range("B:B") would do every row.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,171
Members
452,615
Latest member
bogeys2birdies

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