• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy data from one workbook to another using vba

Siddarth

New Member
Hi all,

I am new to VBA. I want to copy data with in range "A1:E7" to another sheet in a different work book. I have no idea where to start. Can anyone please help?

I have the following code to copy data:
Code:
Dim wb As Workbook
Set wb = ThisWorkbook
Workbooks.Open Filename:="C:\Users\Siddarth\Desktop\Test 0.xlsm"
Range("A1:E7").Select
Selection.Copy

I got no idea how to paste it in the other workbook. The destination file is Test1.xlsx. Please help.

Thanks in advance.
 
Hi,
I hope the following code helps you.

Sub copyrange()
Dim a As Workbook
Dim b As Workbook

Open both workbooks :
Set a = Workbooks.Open(" path from copying book ")
Set b = Workbooks.Open("C:\Users\Siddarth\Desktop\Test 0.xlsm")

'Copy what you want from a:
a.Sheets("name of copying sheet").Range("A1:E7").Copy

'Paste to b worksheet:
b.Sheets("sheetname").Range("A1").PasteSpecial

'Close a:
a.Close

EndSub
 
Thanks for your answer. :)
The code below is what i modified from the one you provided. You know i tried the same thing earlier. What is happening is that the Destination file is "Test 1.xlsx". Data is not pasted there for some reason. I am triggering this code through a command button on Source file which is "Test 0.xlmx".

Code:
Dim a As Workbook
Dim b As Workbook

'Open both workbooks :
Set a = Workbooks.Open("C:\Users\Siddarth\Desktop\Test 0.xlsm")
Set b = Workbooks.Open("C:\Users\Siddarth\Desktop\Test 1.xlsx")

'Copy what you want from a:
a.Sheets("Sheet1").Range("A2:E7").Copy

'Paste to b worksheet:
b.Sheets("Sheet1").Range("A2").PasteSpecial

'Close a:
a.Close

Thanks...
 
Back
Top