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 :) :)

[TABLE="class: outer_border, width: 800"]
<tbody>[TR]
[TD]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
[/TD]
[/TR]
</tbody>[/TABLE]



Thanks in advance
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
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,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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