If B2 contains then delete...

HotLanta

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

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

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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,191,165
Messages
5,985,033
Members
439,935
Latest member
Monty238

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