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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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