Working Environment: Excel 2013
Target: Delete the unnecessary columns by filtering the content in row 2.
My idea is that as long as the content in row 2 is either
Physical LocationorPLC Tag NameorTest Step1/2/3/4/5/6/7,
keep those columns, otherwise delete it.
My problem is that I need to run this macro multiple times to delete all the unnecessary columns. It should loop from 1 to 40, and just leave the columns that I want. I am not sure why it doesn't work. Can anyone help me? Thanks!
My code:
Sub Reorder()
Rows(1).Insert shift:=xlShiftDown
For i = 1 To 40
WY = Worksheets("Sheet4").Cells(2, i)
Select Case WY
Case "Physical Location"
Worksheets("Sheet4").Cells(1, i) = 1
Case "PLC Tag Name"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step1"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step2"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step3"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step4"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step5"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step6"
Worksheets("Sheet4").Cells(1, i) = 1
Case "Test Step7"
Worksheets("Sheet4").Cells(1, i) = 1
Case Else
Worksheets("Sheet4").Cells(1, i) = 0
End Select
Next i
For i = 1 To 40
If Worksheets("Sheet4").Cells(1, i) = 0 Then
Columns(i).EntireColumn.Delete
End If
Next
End Sub