VBA code to clear cell if other cells are changed

virtuosok

Board Regular
Joined
Sep 2, 2020
Messages
209
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am trying to convince the following code work for me:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("A5:A8")) Is Nothing Then
        Range("A2").ClearContents
    End If
End Sub
...but it doesn't. I have saved the file as .xlsm, added the above piece as a "module" (General/Worksheet_Change) but whenever I update values in cells A5:A8, entry in cell A2 is not automatically erased as expected. Please help?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Your code should not be in a Module.
Install it like this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window
 
Upvote 0
Thanks, this works.
Is there a way to enhance the code so that whenever cell A5 (or A8) is updated, cell A2 gets auto-erased, and vice versa? (A2 update erases any A5 and A8 entries).
 
Upvote 0
Hi !​
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        Application.EnableEvents = False
    Select Case Target(1).Address
           Case "$A$2":         [A5,A8].ClearContents
           Case "$A$5", "$A$8": [A2].ClearContents
    End Select
        Application.EnableEvents = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,043
Messages
6,122,812
Members
449,095
Latest member
m_smith_solihull

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