Copy data between workbooks

pallar

New Member
Joined
May 17, 2010
Messages
49
How should i declare this?

I want my makro to copy Say cells A1:A10 in sheet1.

Then open another workbook from path c:\myworkbook2.xlsx

Paste the data in myworkbook2, sheet1 cells B1:B10
Save myworkbook2
Close myworkbook2

And back to sheet1 from where i copied..

Can someone straight this out for me please.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try This:


Code:
Sub test()
    
    Dim wbSource As Workbook
    Dim wbTarget As Workbook
    
    Dim wsSource As Worksheet
    Dim wsTarget As Worksheet
    
    ' If target file is not there
    If Dir("c:\myworkbook2.xlsx", vbNormal) = "" Then
        MsgBox "Target workbook not found as - c:\myworkbook2.xlsx", vbCritical
        Exit Sub
    End If
    
    ' Set Source file objects
    Set wbSource = ActiveWorkbook
    Set wsSource = wbSource.Sheets("Sheet1")
    
    ' Open Target file
    Set wbTarget = Workbooks.Open("c:\myworkbook2.xlsx")
    Set wsTarget = wbTarget.Sheets("sheet1")
    
    ' Copy paste the data from Source to Target
    wsSource.Range("A1:A10").Copy
    wsTarget.Range("B1").PasteSpecial (xlPasteAll)
    Application.CutCopyMode = False
    
    ' Save and close the Target file
    wbTarget.Close (True)
    
    
    ' Set all objects to nothing
    Set wbSource = Nothing
    Set wsSource = Nothing
    Set wbTarget = Nothing
    Set wsTarget = Nothing
        
End Sub
 
Upvote 0
I cant get this to work.. I doublechecked but anyhow i just get the msgbox with the warning that file aint exist. But as i said i doublechecked. :(
 
Upvote 0
If you are getting this message:

"Target workbook not found as - c:\myworkbook2.xlsx"

Means the target file where you want to paste the data, not at the c drive with name - myworkbook2.xlsx.
 
Upvote 0
I just noticied why i got the fault.. My file was named myworkbook2.xlsx.xlsx

=). Works perfect now thanks alot!
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,435
Members
448,961
Latest member
nzskater

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