Hello,
So... I've been working at this for quite a while. I think the code needs a closing statement for the for, but I'm not sure. When the error produces ".EntireRow.Delete" is highlighted; however, it still deleted the rows correctly. I'm trying to make this template neat, so it works, but it would be amazing if it didn't produce the run-time error. So, what would be the best way to fix this?
The macro finds "Term_DT" in the first row and deletes rows which do not contain "0" from that column. If there's a better way to do this please enlighten me.
So... I've been working at this for quite a while. I think the code needs a closing statement for the for, but I'm not sure. When the error produces ".EntireRow.Delete" is highlighted; however, it still deleted the rows correctly. I'm trying to make this template neat, so it works, but it would be amazing if it didn't produce the run-time error. So, what would be the best way to fix this?
The macro finds "Term_DT" in the first row and deletes rows which do not contain "0" from that column. If there's a better way to do this please enlighten me.
Code:
Sub Carrier_STS(ws, Acell, rng, col, Lastrow, colName)
'Value for Deleting Terminated Employees
Dim DelTerm As Range
Dim firstrow As Long
Dim Lrow As Long
Dim CalcMode As Long
Dim ViewMode As Long
With ws
Set Acell = .Range("A1:ZZ1").Find(What:="Term_DT", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False)
'If the column is found, then:
If Not Acell Is Nothing Then
col = Acell.Column
colName = Split(.Cells(, col).Address, "$")(1)
Lastrow = .Range(colName & .Rows.Count).End(xlUp).Row
firstrow = .UsedRange.Cells(2).Row
'This is the range of rows in the column specified.
Set rng = .Range(colName & "2:" & colName & Lastrow)
For Lrow = Lastrow To firstrow Step -1
With .Cells(Lrow, colName)
If Not IsError(.Value) Then
If .Value <> "0" Then .EntireRow.Delete
End If
End With
Next
Else
MsgBox "Could not delete terminated employees!"
End If
End With
End Sub