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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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
Do you have to start the loop from the end, like with deleting rows, so that the code doesn't skip columns?
 
Upvote 0
Thanks!
Can you tell me where to put this information with Excel?
Thanks again,
jonrs
 
Upvote 0
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
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
That one DID IT!!! THANKS!!!
I have been trying to get that to work for a week now!!!
:)
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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