Hello folks:
I posted earlier last week on this code, I have done some work on it but it is still not working as intended. I want to delete all rows in byEmployee and byPosition sheets if the row name is not listed in range A1:A in Sheet1.
Right now, the loop deletes all rows on byEmployee and by Position except for row 1 (which is my header name). I can't get it to work properly.
Any ideas where I am going wrong here?
I posted earlier last week on this code, I have done some work on it but it is still not working as intended. I want to delete all rows in byEmployee and byPosition sheets if the row name is not listed in range A1:A in Sheet1.
Right now, the loop deletes all rows on byEmployee and by Position except for row 1 (which is my header name). I can't get it to work properly.
Any ideas where I am going wrong here?
Code:
Sub delColumnNames()
Dim ColName As String
Dim LastRow As Long
Dim LastCol As Long
Dim RowRange As Range
Dim RowWS As Worksheet
Dim lngLr As Long
Dim WS As Worksheet
Dim i As Long
Dim temp
'List of rows (to keep)
Set RowWS = Sheets("Sheet1")
LastRow = RowWS.Cells(Rows.Count, 2).End(xlUp).Row
Set RowRange = RowWS.Range("A2:A" & LastRow)
For Each WS In Sheets(Array("byEmployee", "byPosition"))
With WS
LastCol = .Cells(Rows.Count, 1).End(xlUp).Row
'Step through rows and delete those not in RowRange list or Blank
For i = LastCol To 1 Step -1
ColName = .Cells(2, i).Value
If ColName = "" Then
'Row is blank - delete it
.Cells(2, LastCol).Rows.EntireRow.Delete
Else
On Error Resume Next
temp = WorksheetFunction.Match(ColName, RowRange, 0)
On Error GoTo 0
'Row not found in row range list - delete it
If IsNumeric(temp) Then
.Cells(1, i).EntireRow.Delete
End If
End If
Next i 'Next column
End With
Next WS
End Sub