Delete columns

S

Scottmk

Hi,
Could someone give me a code that will delete every other colum
between cells B:BA starting with cell B. There are 52 cells betweee
B:BA including them. Don't know if that helps or not. Here is a cod
I tried that basically took a long time before deleting all columns:

Dim i As Long

For i = 52 To 2 Step -1
Columns(i + 1).EntireColumn.Delete
Next i

Since this is a relatively short code, could someone break it down fo
me also? I am really trying to learn this stuff, I have bought 3 book
but I just never feel like reading them after work. Thanks,
Scott
 
D

Don Guillett

I don't know if this is faster. Modify to columns,etc.
For row_index = lastrow To 1 Step -1
If row_index mod 2 =1 then
Rows(row_index).delete
End If
Next
or

Sub DeleteAltCols()
Dim i As Integer
For i = Columns.Count To 1 Step -1
If i Mod 2 = 0 Then Cells(1, i).EntireColumn.Delete
Next
End Sub
or to delete all after selecting.
For i = delRange.Column + 2 To _
Cells.SpecialCells(xlCellTypeLastCell).Column Step 2
Set delRange = Union(delRange, Cells(1, i))
Next i
delRange.EntireColumn.Delete
 
S

Scottmk

Don,
none of those seemed to work for me. It's possible I'm not allowing i
enough time. I have anywhere from 50-150 rows of actual data, and the
all rows underneath (yes, all the way down to row 65536) contains "-".
How long should the module take to run? Based on what I've told you
which would be the fastest?

Thanks, I appreciate any assistanc
 
S

Scottmk

OK, I got one of them to work:

Sub DeleteAltCols()
Dim i As Integer
For i = Columns.Count To 1 Step -1
If i Mod 2 = 0 Then Cells(1, i).EntireColumn.Delete
Next
End Sub

But man, it took like 5 minutes to run. The part where I could tell i
was actually deleting didn't seem to take long at all, but the par
before that took a long time. Any suggestions
 
S

Scottmk

Don,
no, it wasn't a different question. Since you gave me so man
different options on how to handle the situation, I was just giving yo
more information on the sheet I was trying to use it for. ...so that yo
could hopefully recommend the most appropriate code (fastest) to get th
job done.

Thank
 
D

Don Guillett

You should always turn off screen updating and calculation during the macro.
Turn back on after.
 
S

Scottmk

Don,
Actually, I think I have solved all of my problems...thanks to you.
However, you have me puzzled with your last post. I'm not sure wha
you mean by turn off screen updating and calculating during macro...ho
do I do that?

Thanks again for all of your help,
Scot
 
D

Don Guillett

application.screenupdating=false
Application.Calculation = xlCalculateManual
code
application.screenupdating=true
Application.Calculation = xlCalculateautomatic
 
Top