Debugging on .match

stuartchaffey

New Member
Joined
Dec 28, 2021
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi, i hope someone can help. i have the below code that keeps debugging at "newRow = APPLICATION.Match(commCell.Value, newColumnA, 0)", this was working last night but has since stopped, any help appreciated.

'COPIES DATA FROM THE RETURNED SHEET AND PLACES DATA INTO NEW SHEET
Sub MatchAndCopyData()
Dim commWs As Worksheet
Dim newWs As Worksheet
Dim commColumnA As Range
Dim newColumnA As Range
Dim commRow As Long
Dim newRow As Long
' Set references to the relevant sheets
Set commWs = ThisWorkbook.Sheets("Communication")
Set newWs = ThisWorkbook.Sheets("New")
' Set references to the columns
Set commColumnA = commWs.Range("A:A")
Set newColumnA = newWs.Range("A:A")
' Loop through each cell in commColumnA
For Each commCell In commColumnA
If Not IsEmpty(commCell.Value) Then
' Find the matching row in newColumnA
On Error Resume Next
newRow = APPLICATION.Match(commCell.Value, newColumnA, 0)
On Error GoTo 0
If Not IsError(newRow) Then
' Copy data from columns F, G, H, I, J, & K
commWs.Cells(commCell.Row, "F").Resize(1, 6).Copy _
Destination:=newWs.Cells(newRow, "F")
End If
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What is the exact error message?
Are you sure that there is a match?
What are the values of commCell and newColumnA at the time of the error?
 
Upvote 0
I have attached images for you to see
 

Attachments

  • Sheet New.jpg
    Sheet New.jpg
    58 KB · Views: 10
  • Sheet Communication.jpg
    Sheet Communication.jpg
    101.6 KB · Views: 10
  • Error.jpg
    Error.jpg
    120 KB · Views: 10
Upvote 0
Try changing:
VBA Code:
Application.Match.
to:
VBA Code:
Application.WorksheetFunction.Match.
and see if that fixes it.
 
Upvote 0
One issue is you have two "If" lines, and but only one "End If" line.
You are also missing the "Next" that goes with the corresponding "For Each..." line.
 
Last edited:
Upvote 0
Also, the fact that APPLICATION is capitalised suggests you have named a Module, Sub or Function "APPLICATION" - if you have, you should rename it avoiding words that have a meaning in VBA, like "MyApplication"
 
Upvote 0
Hi,
untested but see if this update to your code resolves your issue

VBA Code:
Sub MatchAndCopyData()
    Dim commWs      As Worksheet, newWs As Worksheet
    Dim commColumnA As Range, newColumnA As Range
    Dim commRow     As Long
    Dim newRow      As Variant
    
    ' Set references to the relevant sheets
    Set commWs = ThisWorkbook.Sheets("Communication")
    Set newWs = ThisWorkbook.Sheets("New")
    
    ' Set references to the columns
    Set commColumnA = commWs.Range("A:A")
    Set newColumnA = newWs.Range("A:A")
    
    ' Loop through each cell in commColumnA
    For Each commcell In commColumnA
    
        If Not IsEmpty(commcell.Value) Then
            ' Find the matching row in newColumnA
            newRow = Application.Match(commcell.Value, newColumnA, 0)
            
            If Not IsError(newRow) Then
                ' Copy data from columns F, G, H, I, J, & K
                commWs.Cells(commcell.Row, "F").Resize(1, 6).Copy _
                Destination:=newWs.Cells(CLng(newRow), "F")
            End If
        End If
        
    Next commcell
    
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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