VBA code simple copy data from one workbook sheet and simple paste to another workbook sheet.

Lv4games

New Member
Joined
Oct 7, 2016
Messages
17
Hello everyone,

New to the site.

So I have this repetitive duty where I copy the whole sheets data from one workbook (name AMS RT AMSCOMMAND, sheet (AMS_RT_AMSCOMMAND_Universal) and paste it on to another workbook (name:Mobile Services Dashboard v4 091916(mc), sheet (Cisco Report) . I have the below code but it keeps crashing my excel. I am working with excel 2013. Here is the code that I have so far (if there is a better code I am willing to try), any help would be much appreciated.

Code:
Sub foo3()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

Set x = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\AMS_RT_AMSCOMMAND_Universal Albany.xls")
Set y = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\Mobile Services Dashboard v4 091916(mc).xls")

Set ws1 = x.Sheets("AMS_RT_AMSCOMMAND_Universal")
Set ws2 = y.Sheets("Cisco Report")

With ws1
    .Cells.Copy ws2.Cells
    y.Close True
    x.Close False
End With

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
It seems ws2 is a blank sheet. Is its position in the workbook important? If not, try this code, it works faster but you need to delete 'Cisco Report" from y.

Code:
Sub foo3()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

[COLOR=#574123]Set x = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\AMS_RT_AMSCOMMAND_Universal Albany.xls")[/COLOR]
[COLOR=#574123]Set y = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\Mobile Services Dashboard v4 091916(mc).xls")[/COLOR]

[COLOR=#574123]Set ws1 = x.Sheets("AMS_RT_AMSCOMMAND_Universal")[/COLOR]

With ws1
.Copy after:=y.Sheets(Sheets.Count)
y.Sheets(Sheets.Count).Name = "[COLOR=#574123]Cisco Report[/COLOR]"
End With

y.Close True
x.Close False


End Sub
 
Last edited:
Upvote 0
I tried the code you provided and it still did not work. Is there away we can modify the code were it doesnt open Workbook Y? I guess I just want it to copy whats on workbook X sheet AMS_RT_AMSCOMMAND_Universal over to Workbook Y sheet Cisco Report. The code you provided me still crashes my excel. I put the code exactly how it is on your code.

AMS_RT_AMSCOMMAND_Universal
 
Upvote 0
I had tested the code on my computer (using local files, not networked ones) and, after reading your reply, tested the code again. It worked both times.

When your Excel crashed, did you get any error messages?

You can step through the code and see which line causes the crash.
 
Upvote 0
It looks like its now crashing now, however when it runs the macro it only opens the files but doesn't copy the data over like I want it. Here is the code I currently have in the file.

Code:
Sub foo3()
Dim x As Workbook, y As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet

Set x = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\AMS_RT_AMSCOMMAND_Universal Albany.xls")
Set y = Workbooks.Open("\\cead.prd\data\USERS\PDX0\j6h068\USRDATA\Documents\Projects\DCR\Mobile Services Dashboard v4 091916(mc).xls")

Set ws1 = x.Sheets("AMS_RT_AMSCOMMAND_Universal")
Set ws2 = y.Sheets("Cisco Report")

With ws1
    .Cells.Copy ws2.Cells
    y.Close True
    x.Close False
End With

End Sub
 
Upvote 0
Try this:

.UsedRange.Copy ws2.Cells
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

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