Delete Method of Range Class Failed, Run-time Error 1004

LadiesAndGentlemen

New Member
Joined
Jul 5, 2012
Messages
14
Hi everyone,

This is my first post so I hope someone can help me out. The VBA macro that I have written is essentially a glorified transpose function (the specifics shouldn't be important for my question). Midway through the code I need to delete a large range of cells so that a) the formatting is cleaner, b) the size of my sheet is smaller, and c) so that the "ActiveSheet.UsedRange.Columns.Count" will give me an accurate answer for the number of cells in use (and won't count the ones that I've gotten rid of).

My code works perfectly on smaller sets of about 200,000 total cells but give me a "Delete Method of Range Class Failed" error as well as an "Excel cannot complete the task with available resources" error when I try running it on larger sets of about 2,000,000 total cells. I am using Excel 2007 on Windows XP.

The code I currently have is:

Range(Cells(1, 4), Cells(howLong, howWide)).Delete
where "howLong" and "howWide" are doubles that just give the coordinate of the bottom right-most entry. "howLong" is 846,721 and "howWide" is 4033 but it should be noted that it still doesn't work when these numbers are like 2 and 1000.

Essentially, all I want to do is delete all the columns from 4 to the end of my document. This code also gives me the same error on large sample sets

Range(Cells(1, 4), Cells(1, howWide)).Select
Selection.EntireColumn.Delete

This should be so simple! I don't know what's going on.

Any help would be greatly appreciated. Many thanks in advanced.
 

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.
Try deleting one column at a time. Something like

Code:
For i = howwide To 4 Step -1
    Columns(i).Delete
Next i
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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