Automatically clearing the contens of cell's.

GI30065

New Member
Joined
Apr 5, 2007
Messages
11
If I delete the contens of cell B3 (text),
then I want excel automatically deletes the contens of cell's E3 to M3 (numbers)
How can I do this ?

Regards,
Marc.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This will do just that:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Range("B3").Value = "" Then
Range("E3:M3").ClearContents
    Else:
    End
End If
End Sub

But there are some problems with this solution. You must populate B3 before you can populate any cell in range E3:M3 because as long as the cell B3 is empty, the other contents are removed.

If you are just going to cell B3 and hitting the delete button to clear that cell, you could refine it a bit so that this only fires when the activecell is B3 as follows:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If ActiveCell = Range("b3") And Range("B3").Value = "" Then
Range("E3:M3").ClearContents
    Else:
    End
End If
End Sub


How are you deleting the text in B3? Will it be specific to only these cells?

If you could give a slightly better description of what you're trying to achieve, you may get a better solution. :)
 
Upvote 0
THANK YOU Sooo MUCH :biggrin:

Both scripts do exactly the thing I was looking for.
Although I don't understand the difference between the two.

I need this for B3 -- E3_M3
B4 -- E4_M4 ,...
Until B52 -- E52_M52
Tried to edit the script, but it doesn't work, I'll guess I'm a real dummie.

Best Regards,
Marc.
 
Upvote 0
Actually, the difference is the second version allows you to enter data into the E-M range without it clearing while B is empty. By running the code only when in column B, you don't accidentally enter info in the other columns just to have it disappear when you change cells.

Not a dummy, I had to go back in and edit my response after I realized that it would do that.

This should do the trick. It checks to see if you are in column B (2) then if you delete the contents of the active cell, it clears E-M.

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If ActiveCell.Column = 2 And ActiveCell.Value = "" Then
Range(ActiveCell.Offset(0, 3), ActiveCell.Offset(0, 11)).ClearContents
    Else:
    End
End If
End Sub
 
Upvote 0
Thanks for all the effort you made !!!
It's nice to know that there are still people who want to help others.

The very first script is fine for me, since I have to delete the data anyway.
The thing I want is the first script but for these cells ==>

B3 -- E3=>M3, B4 -- E4=>M4 ,... Until B52 -- E52=>M52

Does this then means I'll have to write the script 52 times ?

Very Best Regards,
Marc.
 
Upvote 0
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cel As Range
    On Error GoTo ErrH
    If Target.Columns.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    If Not Intersect(Target, Range("B3:B52")) Is Nothing Then
        For Each Cel In Target
            If Cel = "" Then
                Cel.Offset(, 3).Resize(1, 9).ClearContents
            End If
        Next
    End If
ErrH:
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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