How can I program a macro to delete columns based on what column header contains? For example, I want to delete entire columns (including header) if the corresponding cell in the header row contains the % symbol.
Delete Whole Column If Header Contains "x"
- pepperell
- Closed
-
-
-
-
Re: Delete Whole Column If Header Contains "x"
Hi,
THis will cycle through the first 10 columns if it finds % in the top cell it will delete the column.
Does that help?
CodeSub DeleteColumns() DIm i as integer, A as range For i = 10 To 1 Step -1 Set a = Cells(1, i).Find(What:="%", LookIn:=xlValues) If Not a Is Nothing Then a.EntireColumn.Delete Next i End Sub
Edit: I think filo's method above is better.
-
Re: Delete Whole Column If Header Contains "x"
thank you!
-
Re: Delete Whole Column If Header Contains "x"
thank you both.
-
Re: Delete Whole Column If Header Contains "x" such as "%"
A compromise that does all columns without checking every column.
-
Re: Delete Whole Column If Header Contains "x"
Quote from Andy PopeA compromise that does all columns without checking every column.
[vba] Dim A As Range
Do
Set A = Rows(1).Find(What:="%", LookIn:=xlValues, lookat:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
[/vba]Now were talking. : D
-
Can this be used to use a value in another cell rather than hard code "%" ? How would this be done?
-
Read the Forum Rules then start your own post.
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!