I think if not required, just use
Long instead of
Integer because the 64-bit system will convert internally to
Long. Save micro seconds there
Avoid using Select but define sheet name as below. This way the program does not require sheet switching thus code run faster.
I try not to use
UsedRange. If not mistaken, unless you save the file, the
UsedRange will still use old value hen you delete rows or columns. Actual value will not show until you saved the file.
Instead of make program to calculate value multiple time whenever required such as in your code
ActiveSheet.UsedRange.Columns.Count (well you used only once here)
just calculate it once into a variable that you can use anytime like I did below. This will make code run faster.
nColumn = wsPaste.Cells(1, Columns.Count).End(xlToLeft).Column
Caution: Deleting column during run like this, you may end up skipping column because counter just count down without knowing that the column reference number has changed. Once column deleted, the reference to the remaining column might change.
The way I would write your code:
VBA Code:
Sub deleteIrrelevantColumns()
Dim currentColumn As Long, nColumn As Long
Dim columnHeading As String
Dim wsPaste As Worksheet, wsInst As Worksheet
Set wsPaste = ActiveWorkbook.Sheets("Paste")
Set wsInst = ActiveWorkbook.Sheets("Instructions")
' Sheets("Paste").Select
wsPaste.Columns("L").Delete
nColumn = wsPaste.Cells(1, Columns.Count).End(xlToLeft).Column
For currentColumn = nColumn To 1 Step -1
columnHeading = ActiveSheet.Cells(1, currentColumn).Value
'CHECK WHETHER TO KEEP THE COLUMN
Select Case True
Case columnHeading = Instructions!AA9, _
columnHeading = Instructions!AA10, _
columnHeading = Instructions!AA11, _
columnHeading = Instructions!AA12, _
columnHeading = Instructions!AA13, _
columnHeading = Instructions!AA14, _
columnHeading = Instructions!AA15, _
columnHeading = Instructions!AA16, _
columnHeading = Instructions!AA17, _
columnHeading = Instructions!AA18, _
columnHeading = Instructions!AA19
'Do nothing
Case Else
'Delete if the cell doesn't contain "Homer"
If Not wsPaste.Cells(1, currentColumn) = "Homer" Then
ActiveSheet.Columns(currentColumn).Delete
End If
End Select
Next
' Sheets("Instructions").Select
End Sub