Why are my values not copying over to another Workbook

dreen

Board Regular
Joined
Nov 20, 2019
Messages
52
I have read other posts and I still can't seem to figure out what I am doing wrong... I have two workbooks, one with the code in it (Sheet1 is the codename of the sheet I want to copy values from) and another workbook defined as "y" in this example. I want to copy over the value of K1 in Sheet1 into cell F5 of workbook y.

Nothing is happening when I hit the Command button (It is an Activex commandbutton, so I have put it in the Worksheet), nothing is even opening. I have double checked the file paths and the sheet names as well, they are correct.

I have also posted screenshots of the code and both workbooks below:


VBA Code:
Private Sub Commandbutton1_Click()

Dim y As Workbook
Set y = Workbooks.Open("FILEPATH\Databases\Test 2.xlsm")

Sheet1.Unprotect "Swarf"
y.Unprotect "Swarf"

y.Sheets("Mytest2").Range("F5").Value = Sheet1.Range("K1").Value

y.Protect "Swarf"
y.Close SaveChanges = True

Sheet1.Protect “Swarf”

End Sub

test1.JPG


workbooktest1.JPG

workbooktest2.JPG
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
1. Is this workbook protected ?
Set y = Workbooks.Open("FILEPATH\Databases\Test 2.xlsm")
If so use this format
Set y = Workbooks.Open(Filename:="C:\Users...", Password:="Swarf")

2. is sheet1 in workbook containing the code ?

Untested - try this
(should not need to unprotect Sheet1 - it is not being amended)
VBA Code:
Private Sub Commandbutton1_Click()
Dim y As Workbook
Set y = Workbooks.Open(Filename:="???............\Databases\Test 2.xlsm", Password:="Swarf")
With y
   .Sheets("Mytest2").Range("F5").Value = Sheet1.Range("K1").Value
   .Password = "Swarf"
   .Save
   .Close False
End With
End Sub
 
Upvote 0
@Yongle Yes workbook "y" was protected, and sheet1 is within the workbook containing the code! Thank you so much, this explanation was really clear and answered my question!! Now how can I expand this to run through F5-F10 and copy K1-K5 for example?

I am now trying the following code with your suggestion (that worked great) to expand this to run through F1-F10 and copy K1-K10 respectively, but my screen freezes. I will also post this as a separate question!

VBA Code:
Private Sub Commandbutton1_Click()
Dim y As Workbook

Set y = Workbooks.Open(Filename:="???............\Databases\Test 2.xlsm", Password:="Swarf")

With y
  For i=1 to 10  
    Do While cells(i,11).Value<>""
 
   .Sheets("Mytest2").Unprotect "Swarf"
   .Sheets("Mytest2").Cells(i,6).Value = Sheet1.Cells(i,11).Value

    Loop
  Next i
 
   .Password = "Swarf"
   .Save
   .Close False
End With
End Sub
 
Upvote 0
@
1. Is this workbook protected ?
Set y = Workbooks.Open("FILEPATH\Databases\Test 2.xlsm")
If so use this format
Set y = Workbooks.Open(Filename:="C:\Users...", Password:="Swarf")

2. is sheet1 in workbook containing the code ?

Untested - try this
(should not need to unprotect Sheet1 - it is not being amended)
VBA Code:
Private Sub Commandbutton1_Click()
Dim y As Workbook
Set y = Workbooks.Open(Filename:="???............\Databases\Test 2.xlsm", Password:="Swarf")
With y
   .Sheets("Mytest2").Range("F5").Value = Sheet1.Range("K1").Value
   .Password = "Swarf"
   .Save
   .Close False
End With
End Sub

@Yongle

Here is the link to my follow-up question so that it can be answered on a different thread:

 
Upvote 0

Forum statistics

Threads
1,214,865
Messages
6,121,988
Members
449,060
Latest member
mtsheetz

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