Problem with Worksheet Event

MaxChef

New Member
Joined
Jun 23, 2009
Messages
11
Hi all,

This code works for me but it has a bad side effect.

If I attempt to highlight data and "CLEAR CONTENTS", it inserts zeros in the cleared cells. It does the same if I run a macro that clears the entire work range.

Any ideas on how I might fix this?

Also, is it possible to run a Select Case statement using a change event such as this?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   On Error Resume Next
   If Range("NONFOOD_DATES", "NONFOOD_CHARGES") Is Nothing Then Exit Sub
    On Error GoTo 0

If Not Intersect(Target, Range("NONFOOD_DATES")) Is Nothing Then
  Application.EnableEvents = False
        Target.Offset(0, 1).Value = "Amex-NonFoods"
        Application.EnableEvents = True
End If

If Not Intersect(Target, Range("NONFOOD_CHARGES")) Is Nothing Then
  Application.EnableEvents = False
        Target.Offset(0, 1).Value = "0"
        Application.EnableEvents = True

End If
End Sub
Thanks!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Maybe:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Range("NONFOOD_DATES", "NONFOOD_CHARGES") Is Nothing Then Exit Sub
    On Error GoTo 0
    
    If WorksheetFunction.CountA(Target) Then
        Application.EnableEvents = False
        If Not Intersect(Target, Range("NONFOOD_DATES")) Is Nothing Then
            Target.Offset(0, 1).Value = "Amex-NonFoods"
        End If
        If Not Intersect(Target, Range("NONFOOD_CHARGES")) Is Nothing Then
            Target.Offset(0, 1).Value = 0
        End If
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 0
Thanks Wigi!

That works fine.

I would have never considered using the worksheetfunction.COUNTA for the target.

Very good idea.

Thanks.

Mark
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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