Help understanding VBA code - Delete unwanted data base on value of first row/cell of column

lj97

New Member
Joined
Jul 11, 2022
Messages
9
Office Version
  1. 365
Platform
  1. 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! 😃
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
VBA Code:
Sub DeleteRedundantData()

'define your array of items to check against.
Mylist = Array("Apple", "Bananna", "Pear", "Orange", "Melon")

'this line finds the last column in Row 1 that contains data on your sheet.  "LC" presumably was used for "Last Column" and will store a column number. ie. if Column "E" is your last column, then LC = 5
LC = Cells(1, Columns.Count).End(xlToLeft).Column


'define a count, starting with your last column, and counting backwards to col.1 (step -1 = count backwards)
For mycol = LC To 1 Step -1

    x = ""   'define x as NULL
    On Error Resume Next  ' this line simply says if you come across an ERROr, ignore it and carry on. Not often much use in VBA code. 

' So this line uses worksheet.match to look at each cell in row 1 (starting lets say cell E5 (Cells(1 (= row) ,mycol (column) = 5 from above example I gave), and compares it with whats in your Array of words (MyList), and the ,"0" on the end says look for an EXACT match.  If it finds a word in cell E5, matching exactly with a word in MyList array, it will give "x" a numerical value representing the number it is in MyList.  eg. if it found "Pear", x would be set at 3 as Pear is 3rd in your list.
    x = WorksheetFunction.Match(Cells(1, mycol), Mylist, 0)

'then as x has been set from NULL to a "numeric value" ie. "3", it is checked to see if it is simply a number or not (if nothing was found, x would still be "" or NULL which is not a number.
if x is NOt a Number, then delete the column number MyCol or "5" = Column "E"
    If Not IsNumeric(x) Then Columns(mycol).EntireColumn.Delete

Next mycol 'then go check whats in column 4.  The reason to count backwards when deleting columns is that the data to the right is effectively shifted left by 1. Meaning if we started with column 1 and deleted col2, col 3 would effectively become col 2 .. and what you are looking for gets quite confused ... eg. it would miss columns when searching.

End Sub
 
Upvote 0
Hope my comments in line above help.
cheers
Rob
 
Upvote 0
I might code the same function a bit differently.

VBA Code:
Sub DeleteRedundantData2()
    Dim LC As Long, MyCol As Long
    
    LC = Cells(1, Columns.Count).End(xlToLeft).Column                    'Count number of columns with data.
    
    For MyCol = LC To 1 Step -1
        Select Case Cells(1, MyCol).Value
            Case "Apple", "Bananna", "Pear", "Orange", "Melon"           'Columns beginning with these words are retained
            Case Else
                Columns(MyCol).Delete                                    'All other columns are deleted
        End Select
    Next MyCol
End Sub
 
Upvote 0
Thanks Rob for going through each line with your comments, greatly appreciated :)

And thank you rlv01 for your re-work! I will have a sus.
 
Upvote 0
VBA Code:
Sub DeleteRedundantData()

'define your array of items to check against.
Mylist = Array("Apple", "Bananna", "Pear", "Orange", "Melon")

'this line finds the last column in Row 1 that contains data on your sheet.  "LC" presumably was used for "Last Column" and will store a column number. ie. if Column "E" is your last column, then LC = 5
LC = Cells(1, Columns.Count).End(xlToLeft).Column


'define a count, starting with your last column, and counting backwards to col.1 (step -1 = count backwards)
For mycol = LC To 1 Step -1

    x = ""   'define x as NULL
    On Error Resume Next  ' this line simply says if you come across an ERROr, ignore it and carry on. Not often much use in VBA code.

' So this line uses worksheet.match to look at each cell in row 1 (starting lets say cell E5 (Cells(1 (= row) ,mycol (column) = 5 from above example I gave), and compares it with whats in your Array of words (MyList), and the ,"0" on the end says look for an EXACT match.  If it finds a word in cell E5, matching exactly with a word in MyList array, it will give "x" a numerical value representing the number it is in MyList.  eg. if it found "Pear", x would be set at 3 as Pear is 3rd in your list.
    x = WorksheetFunction.Match(Cells(1, mycol), Mylist, 0)

'then as x has been set from NULL to a "numeric value" ie. "3", it is checked to see if it is simply a number or not (if nothing was found, x would still be "" or NULL which is not a number.
if x is NOt a Number, then delete the column number MyCol or "5" = Column "E"
    If Not IsNumeric(x) Then Columns(mycol).EntireColumn.Delete

Next mycol 'then go check whats in column 4.  The reason to count backwards when deleting columns is that the data to the right is effectively shifted left by 1. Meaning if we started with column 1 and deleted col2, col 3 would effectively become col 2 .. and what you are looking for gets quite confused ... eg. it would miss columns when searching.

End Sub
apologies, when I wrote "E5" above as the example - I actually meant "E1" (E = col 5, 1 = Row 1 of course..)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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