VBA code to clear contents of adjacent cells based on cell value

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
30
Hi All.

I'm struggling to find the code to clear the contents of cells based on the content of another cell in the same row. For example:

I have a range of A1:C700.

If on any of these given rows, if in column C of that row the cell contains "Depart", I would like to clear the contents of columns A & B in the same row.

This would happen multiple times in the above range.

Thanks in advance all.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
51,780
Office Version
  1. 365
Platform
  1. Windows
Assuming row 1 is a heading row (so C1 will not contain "Depart") the try

VBA Code:
Sub ClearNearDepart()
  Application.ScreenUpdating = False
  With Range("A1:C700")
    .AutoFilter Field:=3, Criteria1:="Depart"
    With .Columns(1).Resize(, 2)
      If .SpecialCells(xlVisible).Count > 2 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlVisible).ClearContents
    End With
    .AutoFilter
  End With
  Application.ScreenUpdating = True
End Sub
 

Forum statistics

Threads
1,148,007
Messages
5,744,319
Members
423,861
Latest member
Ka3EeM

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