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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,344
Office Version
  1. 365
Platform
  1. Windows
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
 

kalak

Active Member
Joined
Jun 10, 2015
Messages
487
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
 

Forum statistics

Threads
1,136,258
Messages
5,674,672
Members
419,520
Latest member
Jennifer4Dillon

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
Top