0

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

enter image description here

2
  • Hi there, and welcome to SuperUser. We're not a "Please write me a script" kind of service. If you share your VBA code, we can look into why things aren't working the way it should or why it gives a certain error. Can you edit your post and add the VBA code?
    – LPChip
    Commented Nov 10, 2020 at 22:03
  • Since in case of C2>E2(sheet1) then you are trying to delete A2:A3000 also If C3>E3(sheet1) then B2:B3000,, in that case .EntireColumn.Delete is not appropriate to work with!! Also better test cell to cell rather than with If Cells(i, "C").Value >= Cells(i, "E").Value Then !! Commented Nov 18, 2020 at 5:08

1 Answer 1

0

The inefficient code I am using right now (works fine)is:

Sub ClearRange()
Dim ws1, ws2 As Worksheet
Set wb = ThisWorkbook
'Sub sbVBS_To_Delete_EntireColumn_C()
With wb.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
           Set ws1 = wb.Sheets("Input")
           'Clear the range
           If i = 2 Then
            ws1.Range("A2:A3000").Clear
           End If
           If i = 3 Then
            ws1.Range("B2:B3000").Clear
           End If
           If i = 4 Then
            ws1.Range("C2:C3000").Clear
           End If
           If i = 5 Then
            ws1.Range("D2:D3000").Clear
           End If
           If i = 6 Then
            ws1.Range("E2:E3000").Clear
           End If
           If i = 7 Then
            ws1.Range("F2:F3000").Clear
           End If
           If i = 8 Then
            ws1.Range("G2:G3000").Clear
           End If
           If i = 9 Then
            ws1.Range("H2:H3000").Clear
           End If
           If i = 10 Then
            ws1.Range("I2:I3000").Clear
           End If
           If i = 11 Then
            ws1.Range("J2:J3000").Clear
           End If
           If i = 12 Then
            ws1.Range("K2:K3000").Clear
           End If
           If i = 13 Then
            ws1.Range("L2:L3000").Clear
           End If
           If i = 14 Then
            ws1.Range("M2:M3000").Clear
           End If
           If i = 15 Then
            ws1.Range("N2:N3000").Clear
           End If
           If i = 16 Then
            ws1.Range("O2:O3000").Clear
           End If
           If i = 17 Then
            ws1.Range("P2:P3000").Clear
           End If
           If i = 18 Then
            ws1.Range("Q2:Q3000").Clear
           End If
           If i = 19 Then
            ws1.Range("R2:R3000").Clear
           End If
           If i = 20 Then
            ws1.Range("S2:S3000").Clear
           End If
           If i = 21 Then
            ws1.Range("T2:T3000").Clear
           End If
           
        End If
    Next i
    On Error GoTo 0
End With
End Sub

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .