vba delete columns based on header value

t6116ks

New Member
Joined
May 13, 2008
Messages
22
Hello,
I would like to call a Sub anywhere in my workbook to delete all columns containing "*" in the header of one specific worksheet "data".
several postings I tried work ok on a few columns, buy my 'data" sheet contains about 200 columns (so the sub gets stuck and I have to abort...)

I would appreciate if you can lead me through it.
Thanks!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
try this

Code:
Option Explicit


Sub DelAsterisk()
    Dim i As Long, lc As Long
    'Assumes header is in row 1
    With Sheets("data")
        .Cells(1, Columns.Count).End(xlToLeft).Column
        For i = lc To 1 Step -1
            If InStr(.Cells(1, i), "*") > 0 Then
                .Cells(1, i).EntireColumn.Delete
            End If
        Next i
    End With
End Sub
 
Upvote 0
Alan, looks like a slight omission in your code?
Rich (BB code):
lc = .Cells(1, Columns.Count).End(xlToLeft).Column
 
Upvote 0
Thank you, Peter. Nice catch. I guess i should test code before posting. :)
 
Upvote 0
Thank you so much Alan and Peter !
It does work, however it takes a long time spinning and afraid with 150+ columns to go through, it seems it is stuck in a loop.
Is there a way to speed up?
Thanks again!
 
Upvote 0
One more key info: If I trigger Macro from a different sheet, it spins for ever and Excel stops responding.
So Cntl-Alt-Del was only way out...

Thanks again for your support!
 
Upvote 0
It does work, however it takes a long time spinning and afraid with 150+ columns to go through, it seems it is stuck in a loop.
150 columns isn't many so it sounds like you have some other code going on or lots of formulas recalculation each time a column is deleted. See if adding the blue sections makes a difference.
Rich (BB code):
Sub DelAsterisk()
    Dim i As Long, lc As Long, lCalc As Long
    'Assumes header is in row 1
    With Application
      lCalc = .Calculation
      .DisplayAlerts = False
      .Calculation = xlCalculationManual
      .EnableEvents = False
    End With
    With Sheets("data")
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        For i = lc To 1 Step -1
            If InStr(.Cells(1, i), "*") > 0 Then
                .Cells(1, i).EntireColumn.Delete
            End If
        Next i
    End With
    With Application
      .DisplayAlerts = True
      .Calculation = lCalc
      .EnableEvents = True
    End With
End Sub
 
Upvote 0
Thanks a lot Peter!
You are absolutely correct. I have another sheet looking at every cell in "data" and making calculation.
So I will have to change my strategy on the calculation.
Thanks again. You guys are awsome!
 
Upvote 0
Great posts by both Alan and Peter. Thanks for those so I, too, can continue to learn.

Peter -- it looks like a small, but key, letter was inadvertently omitted from this part of the code:
Code:
  [COLOR=#0000cd][B]  With Application
      .DisplayAlerts = True
      .Calculation = [/B][/COLOR][COLOR=#FF0000][B]x[/B][/COLOR][COLOR=#0000cd][B]lCalc
      .EnableEvents = True
    End With[/B][/COLOR]

I think the "x" needs to be added. Does that look right?
 
Upvote 0
Peter_SSs code was correct.
lCalc is a variable that was assigned a value with
Code:
[COLOR=#0000CD][B] lCalc = .Calculation[/B][/COLOR]
That stores the current calculation setting at the beginning of the macro & resets to that value at the end.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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