lj97
New Member
- Joined
- Jul 11, 2022
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
VBA Code:
Sub DeleteRedundantData()
Mylist = Array("Apple", "Bananna", "Pear", "Orange", "Melon")
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For mycol = LC To 1 Step -1
x = ""
On Error Resume Next
x = WorksheetFunction.Match(Cells(1, mycol), Mylist, 0)
If Not IsNumeric(x) Then Columns(mycol).EntireColumn.Delete
Next mycol
End Sub
Hi all! I found this code online and it has been extremely useful for me to clear unwanted columns in a dataset based off the value in the first row/cell of a column.
Example below:
The text in the Mylist = Array (i.e. "Apple", Bananna", "Pear", "Orange", "Melon") are the columns to be kept, with these values in the first row of their columns.
If I have a data range with colums whose first row/cell begins with Apple, Bananna, Pear, Orange, Melon, Avocado, Mushroom, Pineapple, Carrot and I deploy the above macro/VBA code, only the columns with first cell values listed in the Mylist = Array will be kept (i.e. Avocado, Mushroom, Pineapple, Carrot columns will be deleted).
Just wondering if someone would be able to break this down for me further and assist me with what is specifically occuring during each line of the code? I understand some of it, but not all.
I have unfortunately neglected to save the website/user I got this code from but will absolutely post a link to it if I can find it!