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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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