Deleting multiple columns based on cell contents

miraj42

New Member
Joined
May 15, 2019
Messages
4
Hi All,

Was directed here by someone after not being able to find a solution online. I have an Excel report that gets sent through to me every week and I always have to unmerge the top two rows so all headings are unmerged and then delete all columns containing the words "Ex VAT", "Inc VAT" "Front" and "COT" (these usually appear in row 2).

Tried having a go at making a VBA script on it but having a tough time getting my head around it. Any help/advice is appreciated. Let me know if you need anything else, first time posting :)

Thanks

miraj42
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Re: Help deleting multiple columns based on cell contents

Hi,

I've wrote this code here assuming that those key words appear in Row 2:

Code:
Sub unmergeAndSort()
    Dim j As Integer
    Dim endColRow2 As Integer
    
    Rows("1:2").Select
        With Selection
            .UnMerge
            .HorizontalAlignment = xlLeft
        End With
        
    endColRow2 = Cells(2, Columns.Count).End(xlToLeft).Column
    For j = 1 To endColRow2
        If Cells(2, j).Value Like "*Ex VAT*" Then
            Columns(j).Delete Shift:=xlToLeft
        ElseIf Cells(2, j).Value Like "*Inc VAT*" Then
            Columns(j).Delete Shift:=xlToLeft
        ElseIf Cells(2, j).Value Like "*Front*" Then
            Columns(j).Delete Shift:=xlToLeft
        ElseIf Cells(2, j).Value Like "*COT*" Then
            Columns(j).Delete Shift:=xlToLeft
        Else
            'Do Nothing
        End If
    Next j
End Sub
 
Upvote 0
Re: Help deleting multiple columns based on cell contents

Thanks a lot for this tyija1995, did the job perfectly. Really appreciate it
 
Upvote 0

Forum statistics

Threads
1,214,868
Messages
6,122,005
Members
449,059
Latest member
mtsheetz

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