Selectively Delete Columns/Rows by content
- 0 0
-
shares
Do you often download or received spreadsheets with a large number of rows/columns that are surplus to requirement?
If so, read on, as these two UDF methods can save a lot of manual filtering and deleting!
Prerequisites
You'll need two additional functions, Ron De Bruin's 'Last' function that picks the last cell in a row/column, and a quick UDF to turn a column number to a letter. I've included both at the end, so just paste them all into a module to use.
A typical example :
Sheets are very often inconsistent in their column naming and / or ordering.
Say there was sensitive data on column D ( TRANS_ID ) but you couldn't be sure the TRANS_ID column was always on Column D, you couldn't programmatically move through several files and remove this, as you might be deleting the wrong column.
Here's where this Sub comes in, you can specify the row to search, and it'll go through all columns in that row until it finds the text, then deletes that column.
In the example workbook, type the column name ( not case sensitive ) an hit the blue button
It'll find whatever column has that ID and remove it.
And here's the code :
So if we had several sheets to loop through, we'd use the code as follows
I the above script , it doesn't matter where it finds the 'TRANS_ID' entry in the header row (1), it will find it and eliminate the entire column.
Here's the full script, including the code for the Column Number / Column Letter conversion.
Download the file with the scripts and testing buttons here :
I'd recommend, as with most of these scripts, that you copy them into your own xlam add-in, rather than running from a macro-enabled workbook.