Runtime Error 1004

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I keep getting a Runtime Error 1004 with a particular macro. The debug option always take me back to the "UsedRange" portion of:

If ws2.Range("A:A" & Rows.Count).End(xlUp).Row > 4 Then ws2.UsedRange.Offset(4).Clear

What's so frustrating is that if I copy and paste the contents to a brand new worksheet, and copy and paste the macro as a new sub, then it works flawlessly. No change in code or worksheet data, just a refreshing of everything else. With no particular relativity, it seems to eventually cause the same Runtime Error 1004, to which the same corrective action will temporarily resolve it.

Does anyone know what could be causing this instability in what seems to be a functional code?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
If you place this line of code just before the one with the error, what does this return?
Code:
MsgBox ws2.UsedRange.Address
 
Upvote 0
I'm surprised it works at all.
Code:
Range("A:A"&rows.Count)
is trying to add the numbers of rows to the complete column.
Try
Code:
If ws2.Range("A" & Rows.count).End(xlUp).Row > 4 Then ws2.UsedRange.Offset(4).Clear
 
Upvote 0
Thanks for the feedback. I will try both out and let you know. (The file is at my work, without real internet access there.)
 
Upvote 0
I'm surprised it works at all.
Code:
Range("A:A"&rows.Count)
is trying to add the numbers of rows to the complete column.
Try
Code:
If ws2.Range("A" & Rows.count).End(xlUp).Row > 4 Then ws2.UsedRange.Offset(4).Clear


Sorry, the A:A was my typo when entering on the forum. It was just "A". Doesn't quite explain why the range is so funny (see next reply).
 
Upvote 0
If you place this line of code just before the one with the error, what does this return?
Code:
MsgBox ws2.UsedRange.Address


I got $A$1:$F$96, but I only expected to get A1:F8. Also, oddly, the 96 kept getting larger every time I ran the macro (I think by 2's), even though it was the exact same set of data in the range every time (A1:F8). Any ideas or resolutions?
 
Upvote 0
Used Range can be a little flaky if you are deleting data or formatting unused sections of your workbook.
What exactly is it that you want to clear?

If we have a clear understanding of that, we can probably offer alternatives that may work better.
 
Upvote 0
Perhaps I am overcomplicating it. This worksheet is essentially a roster of people and some data about them, with a 4 row header. The data is extracted with a macro from another larger roster, and so I want something at the beginning of the extraction macro that wipes this worksheet clean, except for the 4 row header.

Also, if it matters, the extraction macro also applies a signature line 4 blank rows after the last row of new data, and that starts in column C, so counting used rows could be tricky? (I don't know...still a noob).

Thanks!
 
Upvote 0
How about
Code:
ws2.Rows("5:" & Rows.count).Delete
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

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