VBA show cell address from match function

Deimos

New Member
Joined
Jun 4, 2011
Messages
2
Hi,

I'm a first time poster. I'm creating a sub to search a range and locate a cell that contains the same first two letters of a string. Once located I want to show the cell address in a message box. The below code gives the Run time error '91': Object variable or with block variable not set. The line that receives the error is in bold below. I'm unsure where i've gone wrong, could someone please assist me.

Rich (BB code):
Sub testmatch()

Dim extractedtext As String
Dim r As Integer
Dim c As Integer
Dim region As Range

r = 2
c = 2

Do

extractedtext = Sheet2.Cells(r, c).Value

region = Application.Match(extractedtext & "*", Sheet1.Range("C4:R27"), 0)
If Not region Is Nothing Then
MsgBox extractedtext & "Found at" & region
End If

r = r + 1

Loop Until Sheet2.Cells(r, c).Value = ""

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi

Welcome to the Forum.

Firstly, the MATCH function returns a row or column number relative to the start of the Range being searched. It does not return an Address.

Additionally, MATCH will only give you the first occurence.

Here is an alternative approach using the FIND method -

Code:
Sub testmatch()

Dim extractedtext As String
Dim r As Integer
Dim c As Integer
Dim x As Variant
 
r = 2
c = 2
 
Do
 
extractedtext = Sheets("Sheet2").Cells(r, c).Value
 
With Sheets("Sheet1").Range("C4:R27")
    Set x = .Find(extractedtext, LookIn:=xlValues)
    If Not x Is Nothing Then
        firstAddress = x.Address
        Do
            MsgBox extractedtext & " Found at " & x.Address
            Set x = .FindNext(x)
        Loop While Not x Is Nothing And x.Address <> firstAddress
    End If
End With

r = r + 1
 
Loop Until Sheets("Sheet2").Cells(r, c).Value = ""
'
End Sub

It should provide you with all instances where the extracted text is located as the 1st 2 characters.

hth
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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