×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Stopping a worksheet change event

Stopping a worksheet change event

Stopping a worksheet change event

(OP)
Hi, I have a worksheet change event which works fine as is, if new data is pasted into the range.
How can it be stopped if the range is cleared or deleted?

CODE

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Long
Dim rng2 As Long
    With ActiveSheet
        rng = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

On Error Resume Next  'skip all run-time errors

Application.EnableEvents = False

If Target.Column > 4 Then

Range("A1:2" & rng).Copy
With Sheets("WTD")
rng2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Range("A" & rng2).PasteSpecial xlPasteAll
    End With
End If

Application.EnableEvents = True

On Error GoTo 0  'Turn off error trapping and re-allow run time errors

 MsgBox "Daily Data copied to the WTD Tab"

End Sub 

RE: Stopping a worksheet change event

Hi,


How can it be stopped if the range is cleared or deleted?

CODE

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Long
Dim rng2 As Long
Dim t As Range, bEmpty As Boolean

    bEmpty = True

    For Each t In Target
        If Len(t.Value) > 0 Then
           bEmpty = False
           Exit For
        End If
    Next

    If bEmpty Then Exit Sub

    With ActiveSheet
        rng = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

On Error Resume Next  'skip all run-time errors

Application.EnableEvents = False

If Target.Column > 4 Then

Range("A1:2" & rng).Copy
With Sheets("WTD")
rng2 = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Range("A" & rng2).PasteSpecial xlPasteAll
    End With
End If

Application.EnableEvents = True

On Error GoTo 0  'Turn off error trapping and re-allow run time errors

 MsgBox "Daily Data copied to the WTD Tab"

End Sub 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Stopping a worksheet change event

(OP)
Thanks Skip, I was struggling with the logic as the cell was being changed so why wouldn't of the course the change event trigger. Try as I might I couldn't get a workaround to work.
Your solution as ever is simple but elegant.
Cheers.

RE: Stopping a worksheet change event

rng is Long, I have two errors after:
Dim rng As Long
rng = 5
MsgBox ActiveSheet.Range("A1:2" & rng)
MsgBox ActiveSheet.Range("A1:2" & rng).Address

due to range reference syntax. What range do you need to copy?

combo

RE: Stopping a worksheet change event

(OP)
Hi Combo, dont know what happened there as the range is ("A1:C" & rng) it was after midnight here in the UK though sad

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close