Find Last row number for Col matching Pattern

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
2,013
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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,222,569
Messages
6,166,837
Members
452,077
Latest member
hufflefry

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