Home » excel » excel – Clear Content If a Cell Value Changes in the ActiveSheet of a workbook with many sheets

excel – Clear Content If a Cell Value Changes in the ActiveSheet of a workbook with many sheets

Posted by: admin May 14, 2020 Leave a comment

Questions:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B4")) Is Nothing Then
    Range("H59:CP61").ClearContents
End If
End Sub

B4 is a data validation list. I want to clear the range when cell B4 changes.

I tried putting the code in “ThisWorkbook” and under “Module” section.

I want the code to run for all worksheets. Instead of copying and pasting the code for every worksheet, how can I modify the code to work in “ThisWorkbook” or under the “Module” section?

The code should only clear the contents in the activesheet where B4 changes.

How to&Answers:

If you want to implement the Change event on every possible worksheet, then use the _SheetChange event of the workbook.

Put the code here:

enter image description here

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Sh.Range("B4")) Is Nothing Then
        Sh.Range("H59:CP61").ClearContents
    End If
End Sub