VBA to delete a range of data based on a specified cell value

nc_waggoner

New Member
Joined
Sep 2, 2016
Messages
21
I am looking for a VBA code to delete the contents (not formatting) of a range of a row based on the cell value. For example, as seen below, I have number (col A), Name (col B), and Gender (col C). I would like to enter a number in Cell E2 (representing a number in column A) and have it delete the values of the Name and Gender columns. So if I type in 3 and click the button it will delete "Wayne" and "M".


[TABLE="width: 349"]
<colgroup><col><col><col><col><col></colgroup><tbody>[TR]
[TD]Number[/TD]
[TD]Name[/TD]
[TD]Gender[/TD]
[TD][/TD]
[TD]Delete Number[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Stone[/TD]
[TD]M[/TD]
[TD][/TD]
[TD]?[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Jordan[/TD]
[TD]M[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Wayne[/TD]
[TD]M[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Prince[/TD]
[TD]F[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Allen[/TD]
[TD]M[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Here is a VBA solution.

Code:
Option Explicit


Sub DelContents()
    Dim c As Range, rng As Range
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Set rng = Range("A2:A" & lr)
    For Each c In rng
        If c = Range("D2") Then
            c.Offset(0, 1).ClearContents
            c.Offset(0, 2).ClearContents
        End If
    Next c
End Sub
 
Upvote 0
Code:
Sub mgk()
Dim f
Set f = Range("A:A").Find([e2], LookIn:=xlValues, lookat:=xlWhole)
If Not f Is Nothing Then Rows(f.Row).Resize(, 2).Offset(, 1).ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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