Error 424 ruinning my day

Bonbi456

New Member
Joined
Feb 8, 2023
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hi people,

I'm currently writing some code that I want to do many things, but one of them is to loop through the cells A1 to J1 while checking the value, if it is equivalent to some set values, it deletes the entire column. Right now, the code "works" but it stops and gives me an error 424 everytime the code deletes a column. After it does, I need to restart it everytime for it to work. How can I stop this?

This is my current code:

Sub Automation()

Set Mr = Range("A1:J1")
For Each cell In Mr
If cell.Value = "#" Then cell.EntireColumn.Delete
If cell.Value = "Coupler Detached" Then cell.EntireColumn.Delete
If cell.Value = "Coupler Attached" Then cell.EntireColumn.Delete
If cell.Value = "Host Connected" Then cell.EntireColumn.Delete
If cell.Value = "End Of File" Then cell.EntireColumn.Delete
Next


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Bonbi()
   Dim Cl As Range, Rng As Range
   
   For Each Cl In Range("A1:J1")
      Select Case Cl.Value
         Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File"
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
      End Select
   Next Cl
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
 
Upvote 1
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Sub Bonbi()
   Dim Cl As Range, Rng As Range
  
   For Each Cl In Range("A1:J1")
      Select Case Cl.Value
         Case "#", "Coupler Detached", "Coupler Attached", "Host Connected", "End Of File"
            If Rng Is Nothing Then Set Rng = Cl Else Set Rng = Union(Rng, Cl)
      End Select
   Next Cl
   If Not Rng Is Nothing Then Rng.EntireColumn.Delete
End Sub
YEA!! That works! Thank you!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Quick question, if I want the code to do many things, should I try to put it all in the same "Sub" or should I do each step in a different one?
 
Upvote 0
Really depends, there is no "right" way of doing it, just use whatever you are happiest with.
 
Upvote 1

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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