PC Review


Reply
Thread Tools Rate Thread

Copying a value from one workbook to another

 
 
Jamie Jackson
Guest
Posts: n/a
 
      5th Oct 2009
Hi Folks,

I can create a macro that copies a value from one cell to another, but since
this will be part of a nested routine, I'd like to figure out the most
elegant way to perform this one elementary piece. I suspect that the whole
activate > select > copy > activate > select paste thing that a macro creates
isn't the most elegant way to handle this.

Let's say I want to copy the value of:

Workbook: SourceWorkbook.xls
Worksheet: "Source Data Sheet"
Value: value of "C3"

.... to ...

Workbook: "TargetWorkbook.xls"
Worksheet: "Target Data Sheet"
Target Cell: "A2"

What's an elegant, programmatic way to perform this inter-workbook value
assignment?

Thanks,
Jamie


 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      5th Oct 2009
SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy
TargetWorkbook.Sheets("Target Data Sheet").Range("A2")

all on the same line.

Dan

"Jamie Jackson" wrote:

> Hi Folks,
>
> I can create a macro that copies a value from one cell to another, but since
> this will be part of a nested routine, I'd like to figure out the most
> elegant way to perform this one elementary piece. I suspect that the whole
> activate > select > copy > activate > select paste thing that a macro creates
> isn't the most elegant way to handle this.
>
> Let's say I want to copy the value of:
>
> Workbook: SourceWorkbook.xls
> Worksheet: "Source Data Sheet"
> Value: value of "C3"
>
> ... to ...
>
> Workbook: "TargetWorkbook.xls"
> Worksheet: "Target Data Sheet"
> Target Cell: "A2"
>
> What's an elegant, programmatic way to perform this inter-workbook value
> assignment?
>
> Thanks,
> Jamie
>
>

 
Reply With Quote
 
Jamie Jackson
Guest
Posts: n/a
 
      5th Oct 2009
Thanks for the quick reply, Dan! That looks elegant, alright.

I'm not sure how literally to take your snippet, so let me flesh it out a
bit more, for your validation. I'm a super-noob, so if you spot any problems
that you can call out, you'll save me hours of head-scratching.

' I've got to open both Workbooks first, correct?
sourcePathName = "c:\sourcefiles\SourceWorkbook.xls"
targetPathName = "c:\TargetWorkbook.xls"

Workbooks.Open Filename:=sourcePathName, ReadOnly:=True
Workbooks.Open Filename:=targetPathName

' Is this an appropriate way to get a handle on the workbooks?
Dim SourceWorkbook as Workbook
Dim TargetWorkbook as Workbook
Set SourceWorkbook = Workbooks(sourcePathName)
Set TargetWorkbook = Workbooks(targetPathName)

' Perform copy
SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy _
TargetWorkbook.Sheets("Target Data Sheet").Range("A2")

Would you mind looking that over, and giving feedback?

Thanks,
Jamie

"Dan" wrote:

> SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy
> TargetWorkbook.Sheets("Target Data Sheet").Range("A2")
>
> all on the same line.
>
> Dan
>
> "Jamie Jackson" wrote:
>
> > Hi Folks,
> >
> > I can create a macro that copies a value from one cell to another, but since
> > this will be part of a nested routine, I'd like to figure out the most
> > elegant way to perform this one elementary piece. I suspect that the whole
> > activate > select > copy > activate > select paste thing that a macro creates
> > isn't the most elegant way to handle this.
> >
> > Let's say I want to copy the value of:
> >
> > Workbook: SourceWorkbook.xls
> > Worksheet: "Source Data Sheet"
> > Value: value of "C3"
> >
> > ... to ...
> >
> > Workbook: "TargetWorkbook.xls"
> > Worksheet: "Target Data Sheet"
> > Target Cell: "A2"
> >
> > What's an elegant, programmatic way to perform this inter-workbook value
> > assignment?
> >
> > Thanks,
> > Jamie
> >
> >

 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      5th Oct 2009
One possible option would be as follows (you will need to have the source
workbook open for this to work...

Sub CopyEx()
Dim sourcePathName As String
Dim targetPathName As String
Dim SourceWorkbook As Workbook
Dim TargetWorkbook As Workbook

targetPathName = "c:\TargetWorkbook.xls"
Set SourceWorkbook = ActiveWorkbook
Set TargetWorkbook = Workbooks.Open(targetPathName)

' Perform copy
With TargetWorkbook
SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy
..Sheets("Target Data Sheet").Range("A2")
.Save
.Close
End With

End Sub


"Jamie Jackson" wrote:

> Thanks for the quick reply, Dan! That looks elegant, alright.
>
> I'm not sure how literally to take your snippet, so let me flesh it out a
> bit more, for your validation. I'm a super-noob, so if you spot any problems
> that you can call out, you'll save me hours of head-scratching.
>
> ' I've got to open both Workbooks first, correct?
> sourcePathName = "c:\sourcefiles\SourceWorkbook.xls"
> targetPathName = "c:\TargetWorkbook.xls"
>
> Workbooks.Open Filename:=sourcePathName, ReadOnly:=True
> Workbooks.Open Filename:=targetPathName
>
> ' Is this an appropriate way to get a handle on the workbooks?
> Dim SourceWorkbook as Workbook
> Dim TargetWorkbook as Workbook
> Set SourceWorkbook = Workbooks(sourcePathName)
> Set TargetWorkbook = Workbooks(targetPathName)
>
> ' Perform copy
> SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy _
> TargetWorkbook.Sheets("Target Data Sheet").Range("A2")
>
> Would you mind looking that over, and giving feedback?
>
> Thanks,
> Jamie
>
> "Dan" wrote:
>
> > SourceWorkbook.Sheets("Source Data Sheet").Range("C3").Copy
> > TargetWorkbook.Sheets("Target Data Sheet").Range("A2")
> >
> > all on the same line.
> >
> > Dan
> >
> > "Jamie Jackson" wrote:
> >
> > > Hi Folks,
> > >
> > > I can create a macro that copies a value from one cell to another, but since
> > > this will be part of a nested routine, I'd like to figure out the most
> > > elegant way to perform this one elementary piece. I suspect that the whole
> > > activate > select > copy > activate > select paste thing that a macro creates
> > > isn't the most elegant way to handle this.
> > >
> > > Let's say I want to copy the value of:
> > >
> > > Workbook: SourceWorkbook.xls
> > > Worksheet: "Source Data Sheet"
> > > Value: value of "C3"
> > >
> > > ... to ...
> > >
> > > Workbook: "TargetWorkbook.xls"
> > > Worksheet: "Target Data Sheet"
> > > Target Cell: "A2"
> > >
> > > What's an elegant, programmatic way to perform this inter-workbook value
> > > assignment?
> > >
> > > Thanks,
> > > Jamie
> > >
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying workbook formating to a differnent workbook Roger Microsoft Excel Worksheet Functions 0 30th Oct 2009 05:27 PM
Copying range from selected workbook to open workbook John Microsoft Excel Programming 2 11th Aug 2007 03:49 PM
copying worksheets to a new workbook without formulae referencing original workbook pjdeeb@gmail.com Microsoft Excel Programming 2 16th Oct 2006 07:31 PM
Copying data from workbook/sheets to another workbook/sheet =?Utf-8?B?eXVrb25fcGhpbA==?= Microsoft Excel Programming 0 26th Jul 2006 07:33 PM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Microsoft Excel Programming 1 1st Apr 2006 08:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 AM.