Macro to delete specific columns in excel

jesuscares

New Member
Joined
Jun 10, 2015
Messages
25
Hello guys

Is there a way to delete specific columns particularly in ranges for example A: AO then AM: AZ?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

doncarp

Board Regular
Joined
Aug 21, 2004
Messages
55
Office Version
  1. 2016
Platform
  1. Windows
Here is code to delete column A. Just change the "A:A" to the columns you want to delete.

Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
 
Upvote 0

vcoolio

Well-known Member
Joined
Jun 29, 2014
Messages
1,397
Office Version
  1. 365
Platform
  1. Windows
Hello Jesuscares,

Here's another option. You can delete columns by range using an Input Box.


Code:
Sub DelCols()
   Dim Rng As Range
   Dim Cell As Range
   
   On Error Resume Next
   Set Rng = Application.InputBox(Prompt:="Select cells or ranges in the column(s) you wish to delete.", Title:="Select Cells or Ranges", Type:=8)
   If Not Rng Is Nothing Then
        For Each Cell In Rng
             Cell.EntireColumn.Delete
        Next
   End If
End Sub

You can peruse my test work book here.


https://www.dropbox.com/s/tbuj0xvp5a3l4i2/Delete Columns by Range Input Box(2).xlsm?dl=0

There are notes in the text work book to guide you.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0

jesuscares

New Member
Joined
Jun 10, 2015
Messages
25
ADVERTISEMENT
Hello vcoolio

Ok so if I type in A:AO and AM:AZ in the prompt section, this will work? I am also confused with the Title part of the code.
 
Upvote 0

slinky

Active Member
Joined
Dec 19, 2008
Messages
294
Bear in mind that once you delete A:AO, AM is no longer AM.
I always find it's best to delete from right -> left ..
 
Upvote 0

jesuscares

New Member
Joined
Jun 10, 2015
Messages
25
Hello guys. Thanks a lot for the input! I came across something a bit simpler and more tailored to my needs:

Sub DelCols()
Range("A:AK,AM:AZ").EntireColumn.Delete
End Sub

Sorry about the typo slinky! AO was a mistake on my part. However I am getting a code interrupted error with the End Sub statement being highlighted in yellow. Any ideas?
 
Upvote 0

Forum statistics

Threads
1,195,655
Messages
6,010,939
Members
441,577
Latest member
Alonshow

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
Top