Solved

Worksheet_Change event not running when DELETE key is used

Posted on 2010-08-18
14
1,179 Views
Last Modified: 2012-05-10
Hi, Experts:
I've seen a couple of questions like this but none were answered in a way that helped me, so sorry for any redundancy. I need the Worksheet_Change event to delete contents in some cells if a specific cell's contents are deleted. But the code won't run if the cell is changed with the Delete key....this is driving me nuts. Can someone please help? Partial code is below. Thanks!
If Not Intersect(Target, Range("A9:A21")) Is Nothing Then

        'first employee

        If Target.Address = "A9" Then

            If Target = "" Then

                Application.EnableEvents = False

                Cells(Target.Row, Target.Column + 1).Value = ""

                Cells(Target.Row, Target.Column + 2).Value = ""

                Cells(Target.Row, Target.Column + 3).Value = ""

                Cells(Target.Row, Target.Column + 4).Value = ""

                Cells(Target.Row, Target.Column + 5).Value = ""

                Cells(Target.Row, Target.Column + 6).Value = ""

                Cells(Target.Row, Target.Column + 7).Value = ""

            End If

            'more code will follow

            Application.EnableEvents = True

        End If

     End If

Open in new window

0
Comment
Question by:nbozzy
  • 7
  • 6
14 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 33469968
Try this. I took the liberty of shortening your code as well.
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A9:A21")) Is Nothing Then
    'first employee
    If Target.Address = "$A$9" Then
        If Target = "" Then
            Application.EnableEvents = False
            Target.Offset(, 1).Resize(, 7).ClearContents
        End If
        'more code will follow
        Application.EnableEvents = True
    End If
End If

End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:SANTABABY
ID: 33470044
The following code (slightly mofifies your code) worked when I tried ( for change and delete)
Private Sub Worksheet_Change(ByVal Target As Range)

   If Not Intersect(Target, Range("A9:A21")) Is Nothing Then

        'first employee

        If Target.Address = "$A$9" Then

            If Target = "" Then

                Application.EnableEvents = False

                Cells(Target.Row, Target.Column + 1).Value = ""

                Cells(Target.Row, Target.Column + 2).Value = ""

                Cells(Target.Row, Target.Column + 3).Value = ""

                Cells(Target.Row, Target.Column + 4).Value = ""

                Cells(Target.Row, Target.Column + 5).Value = ""

                Cells(Target.Row, Target.Column + 6).Value = ""

                Cells(Target.Row, Target.Column + 7).Value = ""

            End If

            'more code will follow

            Application.EnableEvents = True

        End If

     End If



End Sub

Open in new window

0
 

Author Comment

by:nbozzy
ID: 33474267
Neither approach worked for me, so I am attaching my file (see PerDiem sheet). Two things that might have an affect: Cell A9 is merged with B9:D9, and A9 is populated with a drop-down list.

And StephenJR - your code modification was very interesting to me! Thanks for the new trick!
EXPENSE-REPORT-MASTER---022510.xls
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 500 total points
ID: 33474445
Yes I think it's the merged cells which throws the code. Merging cells is never recommended. If you need the formatting, try Centre Across Selection instead.
0
 

Author Comment

by:nbozzy
ID: 33474615
But then Center Across Selection allows the user to click in any of the 4 cells, which will easily confuse them. Suggestions, please?
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 33474988
No, the change event will only be triggered by A9 changing. My only suggestion is not to merge cells! You may have to rethink your layout but that's a different matter.
0
 

Author Comment

by:nbozzy
ID: 33475059
The layout has been around for years and I inherited it. Is there no way to get the code to work with the cells merged?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Accepted Solution

by:
StephenJR earned 500 total points
ID: 33475257
If you comment out this line,
'If Target.Cells.Count > 1 Then Exit Sub
and change other section to this

If Not Intersect(Target, Range("A9:D9")) Is Nothing Then
            If Target(1, 1) = "" Then

it seems to work. If you need the former restriction, I think you may have to add to each of the other sections. If you need help with that, we may be able to advise on how to re-write the code.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 33475337
Perhaps this works? Untested.
Private Sub Worksheet_Change(ByVal Target As Range)

'PerDiem worksheet, Expense Report Master file
'Populate dollar amounts if Job# and Employee name
'Pays $30

Application.ScreenUpdating = False
Application.DisplayAlerts = False
    
If Not Intersect(Target, Range("E8:K20")) Is Nothing And Target.Count = 1 Then
    'first employee
    If Not Intersect(Target, Range("E8:K8")) Is Nothing Then
        If Cells(Target.Row + 1, "A") <> "" Then
            Cells(Target.Row + 1, Target.Column) = "30"
        Else
            Application.EnableEvents = False
            MsgBox "Please FIRST choose the Employee Name."
            Cells(Target.Row, Target.Column) = ""
        End If
        Application.EnableEvents = True
    End If

    'second employee
    If Not Intersect(Target, Range("E12:K12, E16:K16, E20:K20")) Is Nothing And Target.Count = 1 Then
        If Cells(Target.Row + 1, "A") <> "" Then
            Cells(Target.Row + 1, Target.Column) = 30
        Else
            Application.EnableEvents = False
            MsgBox "Please FIRST choose the Employee Name."
            Cells(Target.Row, Target.Column) = ""
        End If
        Application.EnableEvents = True
    End If
End If
    
'Delete dollar amounts if employee name is deleted
If Not Intersect(Target, Range("A9:A21")) Is Nothing Then
    'first employee
    If Not Intersect(Target, Range("A9:D9")) Is Nothing Then
        If Target(1, 1) = "" Then
            Application.EnableEvents = False
            Target.Offset(, 1).Resize(, 7).ClearContents
        End If
        'more code will follow
    Application.EnableEvents = True
    End If
 End If

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Open in new window

0
 

Author Comment

by:nbozzy
ID: 33475362
StephenJR, that worked! I'm very grateful because I learned TWO things: how to slay the savage merged cells, and why to not use them myself! Thanks a million for your persistence.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 33475381
Good news. That is an important lesson in life!
0
 

Author Comment

by:nbozzy
ID: 33475392
StephenJR: regarding your code (which you snuck in as I was fashioning my reply), this line didn't work: Target.Offset(, 1).Resize(, 7).ClearContents. Instead I commented it out and used my plodding first attempt, which did work. If you have an idea for this cleaner code, I'd be interested. Thanks!
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 33475450
Funny, it worked for me. See if this variation works.

Target(1,1).Offset(, 1).Resize(, 7).ClearContents
0
 

Author Comment

by:nbozzy
ID: 33475521
Oops -- silly me...mine didn't work because I tried to "fix" it for the merged cell logic, and I changed the offset column to 5. Putting it back to 1 worked beautifully. Thanks so much for all the new knowledge!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now