VBA Target.Address for multiple cells

Joined
Jun 13, 2017
Messages
108
Hello, I have the below code that works perfectly if you enter/copy data one cell at a time.
But I copy/paste multiple cells all the time, at which point the code below gives me an error.

Is there a way to adapt the below code to work with multiple cells changing at the same time?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

        Dim x As Integer
        For x = 2 To Sheets("Delivery Schedule").Cells(Rows.Count, 3).End(xlUp).Row

        
        If Target.Column = 3 And Target.Row = x Then
            Dim z As Integer
            For z = 2 To Sheets("Obsolete Codes").Cells(Rows.Count, 1).End(xlUp).Row
                If Target.Value = Sheets("Obsolete Codes").Range("A" & z) Then
                    MsgBox "An obsolete code has been entered through the last action.", vbCritical
                End If
            Next z
            End If
        Next x

End Sub
<code></code>
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
See if this works
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lRow As Long, c As Range, cFound As Range
    lRow = Sheets("Delivery Schedule").Cells(Rows.Count, 3).End(xlUp).Row

If Not Intersect(Target, Range("C2:C" & lRow)) Is Nothing Then
    For Each c In Intersect(Target, Range("C:C"))
        Set cFound = Sheets("Obsolete Codes").Range("A:A").Find(What:=c, LookIn:=xlValues, LookAt:=xlWhole)
         If Not cFound Is Nothing Then
            MsgBox "An obsolete code has been entered through the last action.", vbCritical
            Exit For
        End If
    Next
End If
End Sub
old code is meaningless without an explanation of the requirement.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

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