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

Gavin Harrison

New Member
Joined
May 2, 2017
Messages
34
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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
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
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

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