VBA Code to Clear Contents of Cell based on Another Cell

leonalap24

New Member
Joined
Jan 11, 2015
Messages
11
Hi, I need help writing a code that will clear the contents of cells D10:G10 & I10:M10 if the contents of cell C10 are deleted. I need for the corresponding rows down do D33:G33 & I33:M33 & C33.

I am able to make it work for the first row but not sure how to include the additional rows without referencing each row individually.

Please help.

Thanks in advance.
 
I entered some dummy data in columns D to G and I to M then deleted the data in column C and the appropriate range cleared properly. I didn't get a "runtime error 13 type mismatch" message. Also your macros are protected with a password so I couldn't see what you already had there.
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It looks like you may have been selecting more than one cell at a time in column C. This would generate the error. Try this code. It will allow you to make multiple selections.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C10:C33")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim myRange As Range
    Dim myCell As Range
    Set myRange = Target
    Application.EnableEvents = False
    On Error GoTo errhandler
    For Each myCell In myRange.Cells
        If myCell = "" Then
            Range("D" & myCell.Row & ":G" & myCell.Row).ClearContents
            Range("I" & myCell.Row & ":M" & myCell.Row).ClearContents
        End If
    Next myCell
    Application.EnableEvents = True
    Application.ScreenUpdating = True
errhandler:
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try adding the blue line.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  [COLOR=#0000ff]If target.Count > 1 Then Exit Sub[/COLOR]
...
...
...

Thanks so much! That fixed the problem but now I have another one. I just realized if I select more than one row and clear contents if doesn't clear the corresponding cells. It works great if I only clear contents on one cell. Any ideas?
 
Upvote 0
Try this Worksheet_Change code. You would need to remove the other(s).
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("C10:C33")) Is Nothing Then
    Application.EnableEvents = False
    On Error Resume Next
    Intersect(Range("C10:C33").SpecialCells(xlBlanks).EntireRow, Range("D:G,I:M")).ClearContents
    On Error GoTo 0
    Application.EnableEvents = True
  End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,273
Messages
6,123,987
Members
449,137
Latest member
abdahsankhan

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