Column Delete if Header contains XXX

Erichev

New Member
Joined
Aug 6, 2012
Messages
44
I have a .CSV that I pull from SCCM. I convert it to .XLSX and then begin modifying it. I try to create my own macros when I can, but I seem to be stuck on something that I hope should be relatively simple.

The report has a random number of columns and rows based on the required output. I need to delete all of the columns that contain "Header_Table0_" in Row 1. I can select them and delete them all, but I want automation. I have already done some recording (easiest method for n00bs) for modifying the other portions of the spreadsheet. I have the macro Finding and Removing "Details_Table0_", resizing the cells, adding filters, and sorting based on ComputerName (originally had Details_Table0_ leading) column. I can put all of this at the end of the Header removal macro to perform the entire operation. Unless these steps are all simple too.

[TABLE="width: 500"]
<tbody>[TR]
[TD]Header_Table0_ComputerName[/TD]
[TD]Header_Table0_ComputerManu[/TD]
[TD]Header_Table0_ComputerModel[/TD]
[TD]Details_Table0_ComputerName[/TD]
[TD]Details_Table0_ComputerModel[/TD]
[/TR]
[TR]
[TD]Computer Name[/TD]
[TD]Computer Manufacturer[/TD]
[TD]Computer Model[/TD]
[TD]CCCC2332[/TD]
[TD]20B7[/TD]
[/TR]
[TR]
[TD]Computer Name[/TD]
[TD]Computer Manufacturer[/TD]
[TD]Computer Model[/TD]
[TD]CCCD3323[/TD]
[TD]20UC[/TD]
[/TR]
</tbody>[/TABLE]

Of course, as mentioned, this table can vary in columns it contains and the information in each column. So I can't just look for "Header_Table0_*", "Computer Name", et.al. without having the macro fail at some point (usually the column isn't there that time). Plus I still need to remove the leading now empty columns (which the number can vary). With my recording technique I can't allow for variables.

I will be using this to learn from. So, if side notes can be added to show me what each step is accomplishing that would be wonderful. VBA is much harder to understand than I thought and I've read that using recording can write the worst kinds of macros.

Thanks in advance.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try this:
Code:
Sub MyDeleteColumns()

    Dim lastCol As Long
    Dim col As Long
    
'   Find last column with data in row 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Loop through all columns (going backwards)
    For col = lastCol To 1 Step -1
'       Delete columns with specific start of header
        If Left(Cells(1, col), 13) = "Header_Table0" Then
            Cells(1, col).EntireColumn.Delete
        End If
    Next col
        
End Sub
 
Upvote 0
Try this:
Code:
Sub MyDeleteColumns()

    Dim lastCol As Long
    Dim col As Long
    
'   Find last column with data in row 1
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Loop through all columns (going backwards)
    For col = lastCol To 1 Step -1
'       Delete columns with specific start of header
        If Left(Cells(1, col), 13) = "Header_Table0" Then
            Cells(1, col).EntireColumn.Delete
        End If
    Next col
        
End Sub

Thank you very much!!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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