Error 424: Looping using For - Next

Steve2020

New Member
Joined
Oct 9, 2017
Messages
2
Hi,

New to VBA here, getting error 424 object not found with this code after running, it runs only once perfectly but stops after 1 iteration. Consider "yyy" as all different variables (code I am using is below).

Any help would be appreciated :) :)

Sub DeleteSpecifcColumn()
Set MR = Range("A2:BT2")
For Each cell In MR
If cell.Value = "yyy" Then cell.EntireColumn.Delete
If cell.Value = "yyy" Then cell.EntireColumn.Delete
If cell.Value = "yyy" Then cell.EntireColumn.Delete
If cell.Value = "yyy" Then cell.EntireColumn.Delete
Next
End Sub

<tbody>
</tbody>



Thanks in advance
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
When deleting row or columns, it is always best to loop through your range backwards. Otherwise, deleting the rows/columns shifts the rest of your range over to the left/up, and values will get missed.
Unfortunately, you cannot look through a range backwards. So you need to do it like this:
Code:
Sub DeleteSpecifcColumn()

    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Loop through columns A:BT backwards
    For c = 72 To 1 Step -1
        If (Cells(2, c) = "www") Or (Cells(2, c) = "xxx") _
            Or (Cells(2, c) = "yyy") Or (Cells(2, c) = "zzz") Then
            Columns(c).Delete
        End If
    Next c
        
    Application.ScreenUpdating = True
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,253
Messages
6,123,891
Members
449,131
Latest member
leobueno

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top