Prevent event from firing on range selection

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Prevent event from firing on range selection

Post by Rudi »

Hi,

I have this code:

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    On Error Resume Next
    If Not Intersect(Target, Range("G3:G102")) Is Nothing And Target.Value <> "" Then
        Target.Offset(0, 3).Copy
        Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats
    End If
How can I prevent it from running the copy/paste process if I select a range of cells in the target area (G2:G100) and press delete?
I've tried: If Selection.Cells.Count > 1
And: Intersect(Target, Range("G3:G102")).Cells.Count > 1

No luck!
TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78481
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Prevent event from firing on range selection

Post by HansV »

Try

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
    If Application.CountIf(Target, "=") = Target.Cells.Count Then Exit Sub
    On Error Resume Next
    If Not Intersect(Target, Range("G3:G102")) Is Nothing Then
        Target.Offset(0, 3).Copy
        Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial _
            xlPasteValuesAndNumberFormats
        Application.CutCopyMode = False
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Prevent event from firing on range selection

Post by Rudi »

Excellent :)
TX
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.