Deleting Columns Macro

jonrs

New Member
Joined
Mar 16, 2011
Messages
5
Hello!
I am trying to get a macro to delete all columns with a particular word in the first row. In this case that word is "Label".
I am new to macro's and not real sure how to input the data so a little help on that part would be great to!!
Thanks alot
jonrs
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Sahak

Well-known Member
Joined
Nov 10, 2006
Messages
1,004
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Hi Jonrs,

Try this,

Code:
Sub DelClmns()
    Dim r As Range
    For Each r In Range("A1:IV1")
        If r.Value = "Label" Then r.EntireColumn.Delete
    Next r
End Sub
 
Upvote 0

jmthompson

Well-known Member
Joined
Mar 31, 2008
Messages
966
Do you have to start the loop from the end, like with deleting rows, so that the code doesn't skip columns?
 
Upvote 0

jonrs

New Member
Joined
Mar 16, 2011
Messages
5
Thanks!
Can you tell me where to put this information with Excel?
Thanks again,
jonrs
 
Upvote 0

Dryver14

Well-known Member
Joined
Mar 22, 2010
Messages
2,396
I've just tried this and it does skip columns like rows
 
Upvote 0

jonrs

New Member
Joined
Mar 16, 2011
Messages
5
I think I found the spot...Created a Macro and just copied and pasted it into the Description field.
When I run it, it does something, but the columns are not deleted.
Maybe a slight change is needed??
Thanks again!! Getting there!!!
 
Upvote 0

jonrs

New Member
Joined
Mar 16, 2011
Messages
5
But need it to delete the whole column....
 
Upvote 0

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Try this. To use it, press alt+F11 to open the VBA editor. Then go to Insert>Module. Paste this code in the module window that opens on the right side. Then to run it, while in your worksheet, press alt+F8 to open the Run Macro window, and choose it from the list:

Code:
Public Sub DeleteColumns()
Dim i   As Long, _
    LC  As Long
LC = Cells(1, Columns.Count).End(xlToLeft).column
Application.ScreenUpdating = False
For i = LC To 1 Step -1
    If Cells(1, i).Value = "Label" Then Columns(i).Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0

jonrs

New Member
Joined
Mar 16, 2011
Messages
5
That one DID IT!!! THANKS!!!
I have been trying to get that to work for a week now!!!
:)
 
Upvote 0

Forum statistics

Threads
1,191,386
Messages
5,986,315
Members
440,017
Latest member
vasanrajeswaran

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