If B2 contains then delete...

HotLanta

Board Regular
Joined
Nov 3, 2005
Messages
176
Could someone help me with this code:

If cell B2 contains "Documents and Settings", then Delete column A, B, D, and E.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Sub Test()
On Error GoTo NotFound

If WorksheetFunction.Find("Documents and Settings", Range("B2")) > 0 Then
    Range("A:B").EntireColumn.Delete
    Range("D:E").EntireColumn.Delete
End If

NotFound:
End Sub
 
Upvote 0
hmm, come to think of it, you may want to reverse the Delete lines...

If you delete A and B first, What was in D and E, is now in B and C. So then whent it deletes D and E, it will actually delete whatever was originally in F and G...does that make sense....

so just swap those two lines around so it deletes D and E first...

so instead of

Range("A:B").EntireColumn.Delete
Range("D:E").EntireColumn.Delete

change it to

Range("D:E").EntireColumn.Delete
Range("A:B").EntireColumn.Delete
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,174
Members
448,870
Latest member
max_pedreira

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