Delete Columns with Condition

mucah!t

Well-known Member
Joined
Jun 27, 2009
Messages
593
Hi all,

I'd like to delete [in VBA] columns D:J except for the column with the value "import" in row 2.
The value "import" however is not always in the same row [never in A:C though]

Any ideas?

Excel Workbook
ABCDEFGHIJ
1test
2ghjgohjkhhjopikjghkjuijimportjhkggfjhgl;kj
3
4
5
Sheet1
Excel 2003
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try

Code:
Sub DelCol()
Dim i As Long, Found As Range
Set Found = Columns("D:J").Find(what:="import", LookIn:=xlValues, lookat:=xlWhole)
For i = 10 To 4 Step -1
    If i <> Found.Column Then Columns(i).Delete
Next i
End Sub
 
Upvote 0
Hi,

Try,

Code:
Sub RemoveColumns()

    Dim lngColumn As Long
    
    For lngColumn = 10 To 4 Step -1
        If Cells(2, lngColumn) <> "Import" Then Columns(lngColumn).Delete
    Next lngColumn

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

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