Autocopy between 2 workbooks

F

FrigidDigit

Hi all,

I would like to create code to accomplish the following but as I know very
little any help would be appreciated:

I have 2 workbooks of which the names could differ from time to time
I would like to create a procedure to copy say range A1:H13 from workbook 1
to the same range in workbook 2.
Which commands would I use to switch between the 2 workbooks and how will I
identify them so that I paste to the correct 1?

TIA

FD
 
D

Dave Peterson

Saved from a previous post:

If you want to prompt the user, you could...

Option Explicit
Sub testme()

Dim rngF As Range
Dim rngT As Range

Set rngF = Nothing
On Error Resume Next
Set rngF = Application.InputBox(Prompt:="Select the range", _
Default:=Selection.Areas(1).Address(external:=True), _
Type:=8).Areas(1)
On Error GoTo 0

If rngF Is Nothing Then
MsgBox "Try later"
Exit Sub
End If

Set rngT = Nothing
On Error Resume Next
Set rngT = Application.InputBox(Prompt:="Select the range", Type:=8) _
.Cells(1)
On Error GoTo 0

If rngT Is Nothing Then
MsgBox "Try later"
Exit Sub
End If

rngF.Copy _
Destination:=rngT

Application.CutCopyMode = False

End Sub


You can swap between workbooks by clicking on Window and choosing the workbook
from there.

If you always want to paste into the same address, you could change this line:

rngF.Copy _
Destination:=rngT

to

rngF.Copy _
Destination:=rngT.parent.range(rngf.address)

But you'd still need some way to get that other worksheet.


If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top