VBA copy to different worksheet

nsdcoelho

New Member
Joined
Jul 30, 2014
Messages
10
Hello all,

I've been finding some difficulties in writing simple VBA macros constructed to copy/paste values between different worksheets.
Can someone explain me why the following code does not work if my active worksheet (the worksheet from which I am running the code) is different that sheet1 (I've simplfied at maximum my code so I could try to understand this issue)?
I've been getting the error "Application-defined or object-defined error" but can't figure out why.

Sub copy()

Dim row1 As Integer, row2 As Integer
row1 = 4
row2 = 5
Worksheets("Sheet1").Range(Cells(row1, 2), Cells(row2, 4)).copy

End Sub


Just another question: where is the syntax error in the expression:

Worksheets("Sheet1").Range("B"&row1&":D"&row2).copy

Shouldn't this be equivalent to the line defined above?
Any help would be very appreciated.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Maybe just the spaces between ampersands
Code:
Worksheets("Sheet1").Range("B" & row1 & ":D" & row2).copy
 
Upvote 0
Thank you for your reply.
It turns out that the spaces where the problem, now this is no longer an issue (I thought that VBA automatically introduced the spaces where need). Thank you!
Any hints on the first problem?
 
Upvote 0
I really dont see the first problem :)
But to copy from one worksheet to another, you could do something like

Code:
Worksheets("Sheet1").Range("B" & row1 & ":D" & row2).copy worksheets("Sheet2").range("B1")
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,380
Members
448,955
Latest member
BatCoder

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