Code to look for word and delete entire column

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Hi I need a code to look for the word Sunday which will always be in row 4 and delete the entire column please. Thanks.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
What column will "Sunday" be in ??
 
Upvote 0
Would you like to try this?
Code:
Sub testcode()
Dim e
For Each e In Cells.Rows(4).Cells
If InStr(e, "Sunday") > 0 Then
    Cells.Columns(e.Column).Delete
    Exit For
End If
Next e
End Sub
 
Upvote 0
It only deletes one column at a time. It doesnt loop and do all of the columns with Sunday in row 4.
 
Upvote 0
Try:
Code:
Sub testcode()
Dim e As Long
Application.ScreenUpdating = False
For e = 1 To Cells(4, Columns.Count).End(xlToLeft).Column
  If InStr(Cells(4, e), "Sunday") > 0 Then
    Columns(e).Delete
    e = e - 1
  End If
Next e
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Code:
Sub testcode2()
Dim i As Integer
For i = Cells.Columns.Count To 1 Step -1
If InStr(Cells(4, i), "Sunday") > 0 Then
    Cells.Columns(i).Delete
End If
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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