Error Trying to Delete Multiple Columns

Heder_Santos

New Member
Joined
Jun 5, 2015
Messages
12
Hello everyone

I'm currently having some issues when trying to delete multiple columns in a worksheet, it shows "Error 1004", and stops the macro.

But when I press F8 after the editor highlights that line of code, it works fine and deletes the columns...

Every time I try to do this in the Sub (it happens 4 times more in other worksheets) I get this error and the same thing happens when I press F8.

Could someone please help me? I've searched here and at other forums but I haven't found someone with a problem like this.

Here are the lines of Code im Using:

WSO.Activate 'Activates the Worksheet

Range("A:A, J:M, U:U, AA:AB, AG:AG, AP:AS, AV:AW, AY:CH").EntireColumn.Delete

'I've tried to use delete (with and without "xlToLeft"), tried to reduce the amount of columns, tried to call a different Sub deleting the range, and nothing worked
'Range("A:A, J:M, U:U, AA:AB, AG:AG, AP:AS, AV:AW, AY:BP, BQ:CI").Delete xlToLeft
'Range("A:A, J:M, U:U, AA:AB, AG:AG").Delete xlToLeft
'Range("AG:AJ, AM:AN,AP:BY").Delete xlToLeft


Thanks
 
Try
Code:
Application.Union(Range("A:A"), Range("J:M"), Range("U:U"), Range("AA:AB"), Range("AG:AG"), Range("AP:AS"), Range("AV:AW"), Range("AY:CH")).EntireColumn.Delete
 
Upvote 0
Mark, It WORKED!

Thank you very much!

I didn't knew this property "Application.Union" I'll research it.

Thanks again!

SOLVED
 
Upvote 0
HTH but being honest in your case probably all you need is
Code:
Range("a1,j1:m1,u1,aa1:ab1,ag1,ap1:as1,av1:aw1,ay1:ch1").EntireColumn.Delete

and in the first code I posted you don't actually need the EntireColumn i.e

Code:
Application.Union(Range("A:A"), Range("J:M"), Range("U:U"), Range("AA:AB"), Range("AG:AG"), Range("AP:AS"), Range("AV:AW"), Range("AY:CH")).Delete

will do.
 
Last edited:
Upvote 0
Hello Mark,

Before asking for help here, I also tried to use: Range("a1,j1:m1..."). EntireColumn.Delete as you've suggested and it haven't worked too.

Your first suggestion of "Application.Union" worked smoothly.

Thanks for your help!

Greetings from Brazil,

Heder
 
Upvote 0
I also tried to use: Range("a1,j1:m1..."). EntireColumn.Delete as you've suggested and it haven't worked too.


Are you 100% certain that you used A1 and not A:A like you posted in post #1 because the exact syntax I used in the 1st code posted in post #4 works fine for me on each run I try.

Do me a favour and copy and paste the code below and give it one more try.

Code:
Sub Testagain()
Range("a1,j1:m1,u1,aa1:ab1,ag1,ap1:as1,av1:aw1,ay1:ch1").EntireColumn.Delete
End Sub
 
Upvote 0

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