run-time error 13 after Application.Match

davidmyers

Board Regular
Joined
Jan 29, 2017
Messages
88
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I'm trying to create a comma separated list in colD from terms in colA where colB and colC match.
I have the following code:
Code:
Sub test2()    Dim r1 As Range
    Dim r2 As Range
    Dim cell As Range
    Dim lastrow As Long
    Dim csvTxt As String
    'change Sheet1 to suit
    With ThisWorkbook.Worksheets("Sheet5")
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set r1 = .Range("A2:A" & lastrow)
    End With


    'change Sheet2 to suit
    With ThisWorkbook.Worksheets("Sheet5")
        lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
        Set r2 = .Range("C2:C" & lastrow)
    End With


    For Each cell In r1
        If Not (IsError(Application.Match(cell.Offset(0, 1).Value, r2, 0))) Then
[COLOR=#ff0000]            If r2.Offset(0, 1).Value > " " Then[/COLOR]
                r2.Offset(0, 1) = r2.Offset(0, 1).Value & ", "
            End If
            r2.Offset(0, 1) = r2.Offset(0, 1).Value & cell.Value
        End If
    Next cell
End Sub

At red line I get Run-Time error "13"

What am I doing wrong?

Thanks
David
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try <> to " "
 
Upvote 0
So how do I locate the matching cell in r2 so that I can add to the list in r2.offset(0, 1)?
 
Upvote 0
r1 is a list of 400 key words, with group name in the next column.
r2 is just a list of 30 group names and I want a list of keywords for the group in the cell next to the group name.
 
Upvote 0
Ah i see now. You are going to have to use your match. Because r2 is several cells you can refer to them like this:

r2(1)

for the first cell in the range etc etc. You can use your match to tell you which cell is the correct cell.
 
Upvote 0
Thanks for this Steve, my coding skills are a bit limited, can you give me a code snippet

Thanks
David
 
Upvote 0
Adapting yours so you can see the changes:

Code:
'change Sheet1 to suit
With ThisWorkbook.Worksheets("Sheet5")
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set r1 = .Range("A2:A" & lastrow)
End With

'change Sheet2 to suit
With ThisWorkbook.Worksheets("Sheet5")
    lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set r2 = .Range("C2:C" & lastrow)
    r2.Offset(0, 1) = ""
End With

For Each cell In r1
    If Not (IsError(Application.Match(cell.Offset(0, 1).Value, r2, 0))) Then
        myMatch = Application.Match(cell.Offset(0, 1).Value, r2, 0)
        If r2(myMatch).Offset(0, 1).Value = "" Then
            r2(myMatch).Offset(0, 1) = cell.Value
        Else
            r2(myMatch).Offset(0, 1) = r2(myMatch).Offset(0, 1).Value & ", " & cell.Value
        End If
    End If
Next cell
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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