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".


NumberNameGenderDelete Number
1StoneM?
2JordanM
3WayneM
4PrinceF
5AllenM

<colgroup><col><col><col><col><col></colgroup><tbody>
</tbody>
 

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.
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,214,895
Messages
6,122,128
Members
449,066
Latest member
Andyg666

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