Re: Blank a cell if the contents of another cell deleted

SATXpotter

New Member
Joined
May 17, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a similar situation as mentioned above, on our inventory sheet I have a stock# field (d3), when you enter the stock# it pulls in information and pricing for that unit. I have 3 blank fields, Regular Price K3, Sale Price L3, & Cash Discount N3, these are blank so the salespeople can play with the price to see how it affects our Profit & Margins. What I want is for when the stock# D3 is deleted it will also clear the other 3 fields. When I used the code above it worked perfectly except after clearing the fields it closes Excel completing and then wants to reopen in safe mode. Any idea what would be causing this? I just need it to clear the fields and keep the file open and working so other units can be looked at. Thank you for your help!!
Here is the code I'm using, slightly modified from below...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim RangeA As Range
Set RangeA = Range("D3")
On Error Resume Next
If Application.CountBlank(RangeA) = RangeA.Cells.Count Then
Range("K3:L3,N3").ClearContents
Else
Exit Sub
End If
Set RangeA = Nothing
End Sub

This is what I found on an old thread...

An Excel Function cannot perform an action like clearing the contents of another cell. However you can use the Worksheet_Change event with Visual Basic to perform what you need. Simply copy the following code and open your worksheet then press Alt+F11 and paste the code in the Right-Side open pane. Then close the Visual Basic Editor and test it out.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RangeA As Range
Set RangeA = Range("B1:D10")
On Error Resume Next
If Application.CountBlank(RangeA) = RangeA.Cells.Count Then
Range("A1:A10").ClearContents
Else
Exit Sub
End If
Set RangeA = Nothing
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I couldn't get my Excel to crash (or the macro to do anything) but it seems over complicated for what you require.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D$3" Then
Range("K3:L3,N3").ClearContents
End If
End Sub
 
Upvote 0
If you change cells within a change event, it will trigger the event recursively. You should always disable events temporarily while doing so:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
dim RangeA as range
set RangeA = Range("D3")
If not intersect(Target, RangeA) is nothing then
On Error Resume Next
If Len(RangeA.Value) = 0 Then
application.enableevents = false
Range("K3:L3,N3").ClearContents
application.enableevents = true
End If
end if
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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