Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

excel vba runtime error

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA Runtime error 1004

Excel VBA Runtime error 1004

ResolvedVersion 2010

Gemma has attended:
Excel VBA Intermediate course

Excel VBA Runtime error 1004

Hi, I have a macro which Doug helped me set up, which basically extracts raw data from 6 sheets, combines then into one raw data sheet, and then separates them back out by location.

When I try and run it on my latest monthly report, i get a runtime error 1004, which says the information can't be pasted because the Copy area and the paste area aren't the same size and shape.

I can't see where there is a difference on my latest spreadsheet versus my new one. Can anyone help? I've put the code as below. It's the ActiveSheet.Paste at the bottom that the debugger higlights.

My other question is, if I have a raw data sheet with no data on it, only headings, will this mess up the macro? Is there a line of code I can put in to allow for this?

Many thanks in advance for any help.

Gem.


Sub CreateRawdataTotal()
'Copy data from Raw Data 1 to Total Raw Data including headings
Sheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Total Raw Data"
Sheets("Total Raw Data").Select
Sheets("Raw Data 1").Select
Range("A1").CurrentRegion.Select
Selection.Copy
Sheets("Total Raw Data").Select
Range("A1").Select
ActiveSheet.Paste

'Copy data from Raw Data 2 to Total Raw Data
Sheets("Raw Data 2").Select
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets("Total Raw Data").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste

RE: Excel VBA Runtime error 1004

Hi Gemma,

Below is a variation of your code which includes a line of code which deletes entire rows if it finds empty cells in column A, an If Statement to handle empty cells below the headers and an alternative line for finding the last row of data.

Sheets("Raw Data 2").Select
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Range("A2").Select
If ActiveCell = "" Then
Exit Sub
Else
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy Destination:=Sheets("Total Raw Data").Range("A" & Range("A1048576").End(xlUp).Row + 1)
'The line above is an alternative to find the last line of pasted data if your code below breaks.
End If
'Sheets("Total Raw Data").Select
'Range("A1").Select
'Selection.End(xlDown).Select
'ActiveCell.Offset(1, 0).Range("A1").Select
'ActiveSheet.Paste

I hope this helps!

Kind regards
Marius Barnard
Excel Trainer

RE: Excel VBA Runtime error 1004

Fab, thanks Marius. I'll give that a go this week.

Thanks

Gem.

RE: Excel VBA Runtime error 1004

Hi Marius/Doug

So, i've been having a play with this, but still hitting problems.

I have changed the code as per what I think you meant above, so that it now reads as below pasted below - but I might have misunderstood???!!!!

I now get a runtime error 1004 that says no cells found for the 2nd line we added (Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete).

So, that's my new problem. The old one is still the same, that I get a runtime error where it can't copy and paste into the same area (on the Total Raw Data sheet) as it says the sizing is different. This always happens on the 3rd raw data set where there is usually no data, but it's presumably to do with the pasting?

Any help much appreciated. I sent Doug the spreadsheet last time, but not sure that is workable!! ?

Thanks

Gem.

'Copy data from Raw Data 3 to Total Raw Data
Sheets("Raw Data 3").Select
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Range("A2").Select
If ActiveCell = "" Then
Exit Sub
Else
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy Destination:=Sheets("Total Raw Data").Range("A" & Range("A1048576").End(xlUp).Row + 1)
End If
Sheets("Total Raw Data").Select
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveSheet.Paste

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Quickly create a list of numbers

The fill handle in Excel has many uses. If a number is typed into a cell and then the fill handle is used, the number will be copied from one cell to the next.

If you wish to create a list of numbers that increment by one at a time, hold down Ctrl in conjunction with using the fill handle - this should give you a series of numbers (e.g. 1, 2, 3 etc) rather than copying a single number.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.