VBA: Copy & paste area not the same size error

Barraka

New Member
Joined
Jun 2, 2011
Messages
7
Hi there,

I have the following code:
Code:
Sub test()
Sheets("try1").Select
Range("e2:e" & [e65000].End(xlUp).Row).SpecialCells(xlCellTypeConstants, 23).Copy Sheets("destination").Range("e2:e999").End(xlDown)
End Sub

What it does is copy all the non-empty cells from column E to a different sheet, at the same column.
It gives me an error message (error '1004', copy and paste area are not the same size). However this only happens if the destination column is empty. If I have a value in row 3, the macro works well (but it erases the value I had in E3).

Is there a workaround that problem?

Thanks for your help
 
I tried the whole On Error bit, but afraid I don't quite get it and its late.

Give this a whirl. If in Sheets("try1") there is only 1 cell then it exits the sub. May not get you where you want to be, but maybe mirabeau will help out.

Code:
Sub test()
    Dim rngSrc As Range
    Dim wsSrc As Worksheet: Set wsSrc = Sheets("try1")
    Dim lrSrc As Long: lrSrc = wsSrc.Range("E" & Rows.Count).End(xlUp).Row
    Dim wsDest As Worksheet: Set wsDest = Sheets("destination")
    Dim lrDest As Long: lrDest = wsDest.Range("E" & Rows.Count).End(xlUp).Offset(1).Row
    Application.ScreenUpdating = False
    
    If lrSrc = 1 Then: Exit Sub

    Set rngSrc = wsSrc.Range("E2:E" & lrSrc).SpecialCells(xlCellTypeConstants, 23)
    
    With wsSrc
        If wsDest.Range("E2").Value = "" Then
            rngSrc.Copy
            wsDest.Range("E2").PasteSpecial xlPasteValues
        Else
            rngSrc.Copy
            wsDest.Range("E" & lrDest).PasteSpecial xlPasteValues
        End If
    End With

    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Thanks for your suggestion Jeffrey,

I managed to get what I wanted with the following code:
Code:
Sub Copier()
Dim lastVal As Integer
Dim caisseVal As Integer
caisseVal = 1
Do While caisseVal < 10

lastVal = Sheets("Caisse" & caisseVal).Range("h65000").End(xlUp).Row
If lastVal > 0 Then
Sheets("Caisse" & caisseVal).Range("h2:h" & Sheets("Caisse" & caisseVal).Range("h65000").End(xlUp).Row).Copy
Sheets("DepotCheques").Range("f65000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

lastVal = Sheets("Caisse" & caisseVal).Range("j65000").End(xlUp).Row
If lastVal > 0 Then
Sheets("Caisse" & caisseVal).Range("=j2:j" & Sheets("Caisse" & caisseVal).Range("j65000").End(xlUp).Row).Copy
Sheets("DepotCheques").Range("h65000").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If

I removed the
Code:
.SpecialCells(xlCellTypeConstants, 23).Copy
into simply
Code:
.Copy
because it didn't do the job when there is only 1 or 2 values in the source column, for some reason.
And I added a loop because there a 9 sheets from which the macro needs to do copy-paste operations.

Anyway, I think it works nicely this way, so thanks a lot for your help!
:)
 
Upvote 0
You might want to try to add this to the beginning of your code.

If IsEmpty(Sheets("try1").Range("E2")) Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,223
Members
453,152
Latest member
ChrisMd

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