Unable to Search using Application.WorksheetFunction.Match and Range with Variables

TrickyRick

New Member
Joined
Apr 16, 2015
Messages
2
Using 2013 VBA.

I am trying to search a Range, i.e. D2:D100 for a string. String to match is a Sting Variable that changes, i.e. TktToFind = "100001-". Object is to return the Row to a Long Variable RowCount.

The Range changes so the beginning and ending are contained in string variables, i.e. BeginCell = "D2", EndCell = "D100"

The command used throws error '1004', Method 'Range' of object '_Worksheet' failed.

The Sheet containing the rows to search is set Active.

Private Sub FindRecord()
Dim RowFound As Long
RowFound = Application.WorksheetFunction.Match(TktToFind, Range(BeginCell, EndCell), 0)

Assistance is appreciated.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
try using Application.Match instead and/or do error handling before you run your match function like "On Error Resume Next"

in addition define a variable as a range first and then assign a range to that variable and use it inside match. you may also need to define the worksheet/workbook the range is in depending on where you run the macro from.

so something like

Dim RG as Range

RG = range(BeginCell & ":" & EndCell)
 
Last edited:
Upvote 0
Or use Find instead of Match

Code:
Dim rFound As Range
Set rFound = Range(BeginCell, EndCell).Find(TktToFind, , xlValue, xlWhole)
If Not rFound Is Nothing Then RowFound = rFound.Row - Range(BeginCell).Row + 1
 
Upvote 0
Ok, first the confession! I had the BeginCell and EndCell defined in the wrong place.

After fixing that, if I use either Range(BeginCell, EndCell) or Range(BeginCell & ":" & EndCell) I get Unable to get the Match property of the WorksheeFunction class

I also tried using Set SearchRange = Range(BeginCell, EndCell with Dim SearchRange as Range, and using SearchRange in the statement.
 
Upvote 0
Is the start and end range in the same column?
Match can only look through it if the range is a single column.
Also have you tried Application.Match instead?
 
Last edited:
Upvote 0
Can you post all of your code rather than just the lines you think are causing the problem, sometimes a simple typo elsewhere in the code can cause errors elsewhere.
 
Upvote 0
The 'Unable to get...' error usually means what you are looking for isn't being found where you are looking for it.
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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