Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copying a value from one workbook to another

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


  #2   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Copying a value from one workbook to another

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Copying a value from one workbook to another

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


  #4   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default Copying a value from one workbook to another

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying workbook formating to a differnent workbook Roger Excel Worksheet Functions 0 October 30th 09 05:27 PM
copying worksheets to a new workbook without formulae referencing original workbook [email protected] Excel Programming 2 October 16th 06 07:31 PM
Copying data from workbook/sheets to another workbook/sheet yukon_phil Excel Programming 0 July 26th 06 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 Excel Programming 1 April 1st 06 08:48 PM


All times are GMT +1. The time now is 04:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"