Macro won't run in Excel 2013, works fine in 2010

wdrvx

New Member
Joined
Oct 4, 2016
Messages
17
Hello Guys,

I'm running a macro whose purpose is to copy certain ranges over to another worksheet. This used to work flawlessly in Excel 2007/2010, but for some reason it's giving me 'runtime error 1004' in 2013/2016. What's interesting is that part of the same code is executed just fine up until the last two bits of it.

I'm virtually clueless and would appreciate if someone could look and explain what's wrong with it.

Here's my code:
Code:
Sub copyRange()
Application.ScreenUpdating = False


Set srcSh = ActiveWorkbook.Worksheets("Source")
Set trgSh = ActiveWorkbook.Worksheets("Target")


Dim cont As Range
Dim start As Long
start = 2
For Each cont In srcSh.Range("BA3:BA500")
    If cont = "Yes" Then
        srcSh.Range(Cells(cont.Row, "J"), Cells(cont.Row, "J")).Copy
        trgSh.Range("A" & start).PasteSpecial Paste:=xlPasteAll
        srcSh.Range(Cells(cont.Row, "K"), Cells(cont.Row, "P")).Copy
        trgSh.Range("B" & start).PasteSpecial Paste:=xlPasteValues
        srcSh.Range(Cells(cont.Row, "T"), Cells(cont.Row, "T")).Copy
        trgSh.Range("H" & start).PasteSpecial Paste:=xlPasteValues
        srcSh.Range(Cells(cont.Row, "AK"), Cells(cont.Row, "AY")).Copy
        trgSh.Range("I" & start).PasteSpecial Paste:=xlPasteValues
        start = start + 1
    End If
    Next cont
End Sub

Problem occurs with the last two ranges (T:T & AK:AY), if I remove this bit of code, the rest runs just fine.
Thanks in advance for any advice!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Since you are just copying values (and single cells for the T column), why not use

Code:
'...
trgSh.Range("H" & start).Value = srcSh.Cells(cont.Row, "T").Value
trgSh.Range(trgSh.Cells(start, "I"), trgSh.Cells(start, 23).Value = srcSh.Range(Cells(cont.Row, "AK"), Cells(cont.Row, "AY")).Value
start = start + 1
'...

Note that you also have to refer to the correct wb/sh for the Cells property
 
Upvote 0
Its nothing to do with the excel version far as i can tell. The reason for the apparent randomness is that you have unqualified ranges:

Code:
srcSh.Range(Cells(cont.Row, "J"), Cells(cont.Row, "J")).Copy

This will error should srcSh not be the activesheet at the time the line executes. You need:

Code:
srcSh.Range(srcSh.Cells(cont.Row, "J"), srcSh.Cells(cont.Row, "J")).Copy

or as its just the one cell:

Code:
srcSh.Cells(cont.Row, "J").Copy
 
Upvote 0
Its nothing to do with the excel version far as i can tell. The reason for the apparent randomness is that you have unqualified ranges:

Code:
srcSh.Range(Cells(cont.Row, "J"), Cells(cont.Row, "J")).Copy

This will error should srcSh not be the activesheet at the time the line executes. You need:

Code:
srcSh.Range(srcSh.Cells(cont.Row, "J"), srcSh.Cells(cont.Row, "J")).Copy

or as its just the one cell:

Code:
srcSh.Cells(cont.Row, "J").Copy

That does indeed work, thanks a lot!

Since you are just copying values (and single cells for the T column), why not use

Code:
'...
trgSh.Range("H" & start).Value = srcSh.Cells(cont.Row, "T").Value
trgSh.Range(trgSh.Cells(start, "I"), trgSh.Cells(start, 23).Value = srcSh.Range(Cells(cont.Row, "AK"), Cells(cont.Row, "AY")).Value
start = start + 1
'...

Note that you also have to refer to the correct wb/sh for the Cells property

This seems more reasonable than my current approach also, will give it a shot!

Thanks guys, helpful as always!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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