error 1004 - delete column

LCC

New Member
Joined
Apr 10, 2021
Messages
4
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
Hello team,

I'm not good enough with VBA and I would like your assistance to understand the VBA code below that always get an error 1004.
I have checked and sheet's name is correct, etc. if I add just very few columns, works perfectly.

For example, this code works well:

Sub sbVBS_To_Delete_Specific_Multiple_Columns()
Sheets("Sheet1").Range("A:A,C:C,H:H,K:O,Q:U").EntireColumn.Delete
End Sub

But If I add many columns as per below, I get the error 1004:

Sub sbVBS_To_Delete_Specific_Multiple_Columns()
Sheets("Sheet1").Range("A:A,B:B,C:C,E:E,F:F,H:H,J:J,K:K,L:L,M:M,N:N,O:O,Q:Q,R:R,S:S,T:T,U:U,V:V,W:W,X:X,Y:Y,Z:Z,AA:AA,AB:AB,AC:AC,AD:AD,AE:AE,AF:AF,AG:AG,AH:AH,AI:AI,AK:AK,AL:AL,AN:AN,AO:AO,AP:AP,AQ:AQ,AR:AR,AS:AS,AT:AT,AU:AU,AV:AV,AW:AW,AX:AX,AY:AY,AZ:AZ,BA:BA,BB:BB,BC:BC,BD:BD,BE:BE,BF:BF,BG:BG,BH:BH,BI:BI,BJ:BJ,BK:BK,BL:BL,BM:BM,BN:BN,BO:BO,BP:BP,BQ:BQ,BR:BR,BS:BS,BT:BT,BU:BU,BV:BV,BW:BW,BX:BX,BY:BY,BZ:BZ,CA:CA,CB:CB,CD:CD,CE:CE,CF:CF,CG:CG,CH:CH,CI:CI,CJ:CJ,CK:CK,CL:CL,CM:CM,CN:CN,CO:CO,CP:CP,CQ:CQ,CR:CR,CS:CS,CT:CT,CU:CU,CV:CV,CW:CW,CX:CX").EntireColumn.Delete
End Sub

I much appreciate if you can assist me.

Thank you in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
The error is likely because the string is too long, I believe that it is limited to 255 characters. Where you have consecutive columns, try using A:C,E:F, etc instead.

If it still errors after that, then you would need to break it down into multiple parts. If you do that it will be better to start from the right side of the sheet, otherwise it is likely that the wrong columns will be deleted by the later lines of code.
 
  • Like
Reactions: LCC
Upvote 0
The error is likely because the string is too long, I believe that it is limited to 255 characters. Where you have consecutive columns, try using A:C,E:F, etc instead.

If it still errors after that, then you would need to break it down into multiple parts. If you do that it will be better to start from the right side of the sheet, otherwise it is likely that the wrong columns will be deleted by the later lines of code.
great stuff.
I will try to combine the lines instead one by one.
I will let you know the results soon.

One more question. I cannot increase the limit of 255 to a bigger one?
 
Upvote 0
If there is a criteria for deleting said columns, you could Loop through them....starting from the right and working left !!
 
Upvote 0
One more question. I cannot increase the limit of 255 to a bigger one?
Try a workaround:

1. Put the range address in a string variable, it can be more than 256 characters.
2. Use split & Union to get the range

something like this:
VBA Code:
Sub a1167744a()
Dim rng As Range
Dim x, arr
Dim txt As String

txt = ("A:A,B:B,C:C,E:E,F:F,H:H,J:J,K:K,L:L,M:M,N:N,O:O,Q:Q,R:R,S:S,T:T,U:U,V:V,W:W,X:X,Y:Y,Z:Z,AA:AA,AB:AB,AC:AC,AD:AD,AE:AE,AF:AF,AG:AG,AH:AH,AI:AI,AK:AK,AL:AL,AN:AN,AO:AO,AP:AP,AQ:AQ,AR:AR,AS:AS,AT:AT,AU:AU,AV:AV,AW:AW,AX:AX,AY:AY,AZ:AZ,BA:BA,BB:BB,BC:BC,BD:BD,BE:BE,BF:BF,BG:BG,BH:BH,BI:BI,BJ:BJ,BK:BK,BL:BL,BM:BM,BN:BN,BO:BO,BP:BP,BQ:BQ,BR:BR,BS:BS,BT:BT,BU:BU,BV:BV,BW:BW,BX:BX,BY:BY,BZ:BZ,CA:CA,CB:CB,CD:CD,CE:CE,CF:CF,CG:CG,CH:CH,CI:CI,CJ:CJ,CK:CK,CL:CL,CM:CM,CN:CN,CO:CO,CP:CP,CQ:CQ,CR:CR,CS:CS,CT:CT,CU:CU,CV:CV,CW:CW,CX:CX")

arr = Split(txt, ",")

    For Each x In arr
        If Not rng Is Nothing Then
            Set rng = Union(rng, Range(x))
        Else
            Set rng = Range(x)
        End If
    Next

rng.EntireColumn.Delete

End Sub
 
  • Like
Reactions: LCC
Upvote 0
This has been solved my case once the Excel, there are too much useless column that's no necessary:

Sub sbVBS_To_Delete_Specific_Multiple_Columns()
Sheets("Sheet1").Range("A:B,D:E,G:G,I:N,P:AH,AJ:AK,AM:CA,CC:CU").EntireColumn.Delete
End Sub

I mean, simply and just a line.
I will jump now to the next step guys.
Thank you very much for the ideas and assistance.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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