Delete Columns

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Could someone explain why this coed to delete every other column does not work.

Sub delete_every_other_column()

lastcol = Cells(1, columns.Count).End(xlToLeft).Column
For c = lastcol To 2 Step -2
Cells(1, c).EntireColumn.Delete
Next c

End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try:
Code:
Sub delete_every_other_column()
    Dim lastcol As Long
    lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
    For c = lastcol To 2 Step -2
        lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
        Cells(1, c).EntireColumn.Delete
    Next c
End Sub
 
Upvote 0
Hi @billandrew
Can you explain why you think that your code is not working, as it does delete every other column.
@mumps
recalculating lastcol every iteration shouldn't make any difference.
 
Last edited:
Upvote 0
You are correct.

deletes every other columns if there is data in every column. In my data set the columns are blank every other...
 
Upvote 0
If you mean you want to delete the blank columns.
This will delete those columns where Row 1 is blank
Code:
Sub DelBlankCols()
    
    Rows(1).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
    
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,225,743
Messages
6,186,773
Members
453,370
Latest member
juliewar

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