Copy from one workbook to another

dummies

New Member
Joined
Jul 10, 2020
Messages
28
Office Version
  1. 2016
Platform
  1. Windows
Hello friends help me with Macro to copy from one work book to another.
I have two workbook.
1. Students
2. Subject
. I wanted to copy row D from subject workbook to row D in students workbook.

What i have tried is:
Workbook("subject").sheets("detail").range("d2:d40").copy_
Workbook("students").sheets("detail").range("d2:d40")
But this is giving me slme error(script out of range)

Thank you
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Two things:

1. It is "Workbooks", not "Workbook".
2. You need the file extension in the file name, i.e.

VBA Code:
Workbooks("subject.xlsm").Sheets("detail").Range("d2:d40").Copy _
Workbooks("students.xlsm").Sheets("detail").Range("d2:d40")

(be sure to change to the proper file extension to match your situation)
 
Upvote 0
Solution
Two things:

1. It is "Workbooks", not "Workbook".
2. You need the file extension in the file name, i.e.

VBA Code:
Workbooks("subject.xlsm").Sheets("detail").Range("d2:d40").Copy _
Workbooks("students.xlsm").Sheets("detail").Range("d2:d40")

(be sure to change to the proper file extension to match your situation)
Thank you very much joe4
<code>
Sub movefiles()
Dim myFile As String

With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show

If myFile = False Then
MsgBox "No file selected.", vbExclamation, "Sorry!"
Exit Sub
Else
Workbooks("subject.xlsm").Sheets("details").Range("d2:d40").Value = Workbooks("students.xlsm").Sheets("details").Range("d2:d40")
End If

End With
End Sub
</code>

I tried in this way but i get runtime error 5.
Thank you.
 
Upvote 0
I do not see any command to actuallyopen the file in your filr dialog script.
I am pretty sure that you need to actually open the file before you can copy and paste into it like that.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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