copy/paste cell in vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to learn how to copy and paste a cell in vba. First, I thought it is as easy as

Cells(1,1).copy
cells(2,2).paste

That would copy cells(1,1) and paste it into cells(2,2) but that did not work. It leads me to a question on why I have cell.paste function?
So I went to watch a video and in that video the author did this

Range("A1").copy Range("C1)

That would copy A1 to C1

So I tried almost the same thing but did not work, please see the code below

Code:
Sub copypaste()
Dim x As Integer
    Dim y As Integer
    Dim n As Integer
    Dim m As Integer
        x = InputBox("first cell row")
        y = InputBox("first cell col")
        n = InputBox("second cell row")
        m = InputBox("second cell col")
    ThisWorkbook.ActiveSheet.Cells(x, y).Copy ThisWorkbook.ActiveSheet.Cells(n, m)
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Your code works just fine for me.
Did you put it in a Generic/General module, or a Sheet or Workbook module?

By the way, I would simply this:
Code:
ThisWorkbook.ActiveSheet.Cells(x, y).Copy ThisWorkbook.ActiveSheet.Cells(n, m)
to this:
Code:
Cells(x, y).Copy Cells(n, m)
as if you leave that stuff off, it will default to the ActiveSheet in the Active Workbook.
 
Upvote 0
Thanks for your reply. I got same error message

Run-time error 1004
copy method of range class failed
 
Upvote 0
Just wanted to add. It did work if I use range instead of Cells!
 
Upvote 0
What are the values of your variables when you get the errors?
 
Upvote 0
Thank you all for the help. It is working now. My fault. I had like 6 cells merged as one and I was trying to copy to one of them. I used different range and it works fine. thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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