Error 1004- Unable to get the matched property

Murtuz

New Member
Joined
Feb 11, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Running below code and getting "1004" error "Unable to match the property".

I have below coding to match the headings from a sheet and paste the data in different sheet under same heading. but when it does not find exact match it get stuck at code "m = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Headers").Range("A" & i).Value, Rng1, 0)"

I just need it to skip the mismatched heading and search for next column heading and paste the data if it exact match.

Kindly help!

VBA Code:
Sub CopyHeaders()

LastRow1 = ThisWorkbook.Worksheets("Headers").Range("A" & Rows.Count).End(xlUp).Row
LastRow2 = ThisWorkbook.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow1

Set Rng1 = ThisWorkbook.Worksheets("Sheet1").Range("A1:AJ1")
Set Rng2 = ThisWorkbook.Worksheets("Sheet2").Range("A1:AJ1")

m = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Headers").Range("A" & i).Value, Rng1, 0)
ThisWorkbook.Worksheets("Sheet1").Range("A2:A" & LastRow2).Columns(m).Copy


h = Application.WorksheetFunction.Match(ThisWorkbook.Worksheets("Headers").Range("A" & i).Value, Rng2, 0)

ThisWorkbook.Worksheets("Sheet2").Range("A2").Columns(h).PasteSpecial xlPasteValues

Next i

MsgBox "Data has been pasted successfully"

End Sub
 

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.
If you use Application.Match instead of WorksheetFunction.Match you can use commands such as IfError and IsError, the worksheet function version forces you to rely on VBA error trapping.

Try something like this:
(you can dispense with the Else part since you want to do nothing OR reverse the logic and go with Exit For)

VBA Code:
    With Application
        m = .IfError(.Match(ThisWorkbook.Worksheets("Headers").Range("A" & i).Value, Rng1, 0), 0)
    End With
    
    If m <> 0 Then
        Debug.Print "Do stuff"
    Else
        Debug.Print "Empty"
    End If
 
Upvote 0

Forum statistics

Threads
1,223,641
Messages
6,173,506
Members
452,518
Latest member
SoerenB

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