I am working with an excel file where in Sheet1, I need to compare column C and Column E. If in any row of sheet 1, Column C value> Column E value, then I need to delete certain column in sheet 2.
For example: If C2>E2(sheet1), I have to delete A2to A3000 of Sheet 2 If C3>E3(sheet1), I have to delete B2to B3000 of Sheet 2 and so on.
Sometimes, I may have N/A in C and E columns. But, I have to ignore these cells in case I have any error.
I have tried this code, but it does not delete columns in sheet 2. What did I do wrong?
Sub sbVBS_To_Delete_EntireColumn_C()
With ActiveWorkbook.Worksheets("Calculation")
'Worksheets("Calculation").Activate
Dim N As Long, i As Long
N = Cells(Rows.Count, "E").End(xlUp).Row
On Error Resume Next
For i = N To 2 Step -1
If Cells(i, "C").Value >= Cells(i, "E").Value Then
ActiveWorkbook.Worksheets("Input").Columns(i - 1).EntireColumn.Delete
End If
Next i
On Error GoTo 0
End With
End Sub
.EntireColumn.Delete
is not appropriate to work with!! Also better test cell to cell rather than withIf Cells(i, "C").Value >= Cells(i, "E").Value Then
!!