?
Solved

VBA - Clear contens of cell, if other cells change

Posted on 2014-02-19
3
Medium Priority
?
2,741 Views
Last Modified: 2014-02-19
Hello Experts,

I am using some VBA code, but it doesn't appear to be working/running? (I have no idea how to verify its even executing)

Here is the code...

Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Address = "pmDimensions" Then
            Range("pmSeal").Value = ""
        End If
    End With
End Sub

Open in new window


pmDimensions consists of 3 cells.   Basically, if any value changes, then I want to clear the contents of pmSeal.

Any ideas what is wrong with the code above?
0
Comment
Question by:Geekamo
  • 2
3 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39870815
You have to turn off events for the code to behave well:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    With Target
        If .Address = "pmDimensions" Then
            Range("pmSeal").Value = ""
        End If
    End With
    Application.EnableEvents = True
End Sub

Kevin
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 39870819
Also, the Address property is not the same as the range name.

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Not Intersect(Target, Range("pmDimensions")) Is Nothing Then
        Range("pmSeal").Value = ""
    End If
    Application.EnableEvents = True
End Sub

Kevin
0
 
LVL 1

Author Closing Comment

by:Geekamo
ID: 39870855
This worked great - thanks Kevin!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

864 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