copying and pasting data from one workbook to another

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi, i Have the code below where i want to copy data from "!UPDATEDnew!JM on day (sheet Northants)"and paste into "LATE JM on day (sheet Northants & Bucks)", but the code below isnt working and all it does is close my spreadsheets down. Please can you help me please?

Code:
Private Sub CommandButton7_Click()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant

'## Open both workbooks first:
Set x = Workbooks.Open("C:\Users\s21375\Desktop\VBA stuff\!UPDATEDnew!JM on day.xlsm")
Set y = Workbooks.Open("C:\Users\s21375\Desktop\VBA stuff\LATE JM on day.xlsm")

'Store the value in a variable:
vals = x.Sheets("Northants").Range("A1").Value

y.Sheets("Northants & Bucks").Range("A1").Value = vals


End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
i only closed the one (LATE JM on the day) wchih is without the command button i didnt close the other as i wouldnt be able to press the command button
 
Upvote 0
hi i just tried something else. i opened up a blank excel file and added a command button with the code and it worked, but it only copied across the one line A29:J29 but it didn't copy down. is there any way it can do this and is there any way i can have the button on my main excel spreadsheet which i copy from?
 
Upvote 0
Try
Code:
Private Sub CommandButton2_Click()
Dim x As Workbook
Dim y As Workbook
Dim vals As Variant

Set x = ThisWorkbook
Set y = Workbooks.Open("C:\Users\s21375\Desktop\VBA stuff\LATE JM on day.xlsm")

With x.Sheets("Northants")
   Intersect(.UsedRange, .Range("29:" & .Rows.Count)).Copy y.Sheets("Northants & Bucks").Range("A1")
End With

End Sub
 
Upvote 0
Yes :) :) that works thank you so much, sorry for the long hall in this, it works great now thank you for your patience
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,836
Messages
6,181,250
Members
453,026
Latest member
cknader

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