PC Review


Reply
Thread Tools Rate Thread

Copy paste a range from one Workbook to another

 
 
Kc-Mass
Guest
Posts: n/a
 
      12th Dec 2006
Hi

I do most of my work in Access but am now working on a reporting system that
is about 2/3rds access and 1/3rd Excel. What I am trying to do is open two
Excel files in Excel 2003 and copy a range from workbook1, sheet 1 to
workbook2, sheet1.

I am getting a failure on the "Paste" operation in the second WorkBook.

Any and all ideas appreciated.

Here is my non working code so far.

Sub ExcelRangeChange()
'Set an instance of Excel and pointers for workbooks and sheets
Dim xlApp As Excel.Application
Dim xlBook1 As Excel.WorkBook
Dim xlBook2 As Excel.WorkBook
Dim xlSheet1 As Excel.Worksheet
Dim xlSheet2 As Excel.Worksheet
Set xlApp = New Excel.Application
Set xlBook1 = xlApp.Workbooks.Open("C:\Documents and Settings\Barbara\My
Documents\WkBk1.xls")
Set xlSheet1 = xlBook1.Worksheets("Sheet1")
Set xlBook2 = xlApp.Workbooks.Open("C:\Documents and Settings\Barbara\My
Documents\WkBk2.xls")
Set xlSheet2 = xlBook2.Worksheets("Sheet1")
xlApp.Visible = True

xlSheet2.Range("B1:B17").Select
xlSheet2.Range("b1:b17").Cut
xlSheet2.Range("E1").Select
xlSheet2.Paste
Workbooks("wkBk1.xls").Activate
Worksheets("Sheet1").Activate
xlSheet1.Range("B1:B17").Select
xlSheet1.Range("B1:B17").Cut
xlSheet1.Range("E1").Select
ActiveSheet.Paste

Workbooks("WkBk2.xls").Activate
Worksheets("Sheet1").Activate
xlSheet2.Range("b1").Select
ActiveSheet.Paste
xlSheet2.Range("e1:e17").Select
xlSheet2.Copy
Workbooks("WkBk1.xls").Activate
xlSheet1.Range("b1").Select
ActiveSheet.Paste

Kevin C


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFydGluIEZpc2hsb2Nr?=
Guest
Posts: n/a
 
      12th Dec 2006
Kevin:

Try this.

I've cut out the selects and activates that weren't needed.

Generally a cut only works once when you paste so use a copy and then cut if
needed later (here you don't as you paste over it).

Sub ExcelRangeChange()
'Set an instance of Excel and pointers for workbooks and sheets
Dim xlBook1 As Excel.Workbook
Dim xlBook2 As Excel.Workbook
Dim xlSheet1 As Excel.Worksheet
Dim xlSheet2 As Excel.Worksheet

Set xlBook1 = Workbooks.Open( _
"C:\Documents and Settings\Barbara\My Documents\WkBk1.xls")
Set xlSheet1 = xlBook1.Worksheets("Sheet1")
Set xlBook2 = Workbooks.Open( _
"C:\Documents and Settings\Barbara\My Documents\WkBk2.xls")
Set xlSheet2 = xlBook2.Worksheets("Sheet1")

' xlApp.Visible = True

xlSheet2.Range("B1:B17").Cut xlSheet2.Range("E1")
xlSheet1.Range("B1:B17").Copy ' use copy and then cut if needed
xlSheet1.Paste xlSheet1.Range("E1")
xlSheet2.Paste xlSheet2.Range("B1")
xlSheet2.Range("E1:E17").Copy xlSheet1.Range("B1")

Application.CutCopyMode = False
End Sub

--
Hope this helps
Martin Fishlock
Please do not forget to rate this reply.


"Kc-Mass" wrote:

> Hi
>
> I do most of my work in Access but am now working on a reporting system that
> is about 2/3rds access and 1/3rd Excel. What I am trying to do is open two
> Excel files in Excel 2003 and copy a range from workbook1, sheet 1 to
> workbook2, sheet1.
>
> I am getting a failure on the "Paste" operation in the second WorkBook.
>
> Any and all ideas appreciated.
>
> Here is my non working code so far.
>
> Sub ExcelRangeChange()
> 'Set an instance of Excel and pointers for workbooks and sheets
> Dim xlApp As Excel.Application
> Dim xlBook1 As Excel.WorkBook
> Dim xlBook2 As Excel.WorkBook
> Dim xlSheet1 As Excel.Worksheet
> Dim xlSheet2 As Excel.Worksheet
> Set xlApp = New Excel.Application
> Set xlBook1 = xlApp.Workbooks.Open("C:\Documents and Settings\Barbara\My
> Documents\WkBk1.xls")
> Set xlSheet1 = xlBook1.Worksheets("Sheet1")
> Set xlBook2 = xlApp.Workbooks.Open("C:\Documents and Settings\Barbara\My
> Documents\WkBk2.xls")
> Set xlSheet2 = xlBook2.Worksheets("Sheet1")
> xlApp.Visible = True
>
> xlSheet2.Range("B1:B17").Select
> xlSheet2.Range("b1:b17").Cut
> xlSheet2.Range("E1").Select
> xlSheet2.Paste
> Workbooks("wkBk1.xls").Activate
> Worksheets("Sheet1").Activate
> xlSheet1.Range("B1:B17").Select
> xlSheet1.Range("B1:B17").Cut
> xlSheet1.Range("E1").Select
> ActiveSheet.Paste
>
> Workbooks("WkBk2.xls").Activate
> Worksheets("Sheet1").Activate
> xlSheet2.Range("b1").Select
> ActiveSheet.Paste
> xlSheet2.Range("e1:e17").Select
> xlSheet2.Copy
> Workbooks("WkBk1.xls").Activate
> xlSheet1.Range("b1").Select
> ActiveSheet.Paste
>
> Kevin C
>
>
>

 
Reply With Quote
 
Kc-Mass
Guest
Posts: n/a
 
      13th Dec 2006
A thousand "Thank You's"


"Martin Fishlock" <(E-Mail Removed)> wrote in message
news:36EA93F2-BCDE-450F-AC4B-(E-Mail Removed)...
> Kevin:
>
> Try this.
>
> I've cut out the selects and activates that weren't needed.
>
> Generally a cut only works once when you paste so use a copy and then cut
> if
> needed later (here you don't as you paste over it).
>
> Sub ExcelRangeChange()
> 'Set an instance of Excel and pointers for workbooks and sheets
> Dim xlBook1 As Excel.Workbook
> Dim xlBook2 As Excel.Workbook
> Dim xlSheet1 As Excel.Worksheet
> Dim xlSheet2 As Excel.Worksheet
>
> Set xlBook1 = Workbooks.Open( _
> "C:\Documents and Settings\Barbara\My Documents\WkBk1.xls")
> Set xlSheet1 = xlBook1.Worksheets("Sheet1")
> Set xlBook2 = Workbooks.Open( _
> "C:\Documents and Settings\Barbara\My Documents\WkBk2.xls")
> Set xlSheet2 = xlBook2.Worksheets("Sheet1")
>
> ' xlApp.Visible = True
>
> xlSheet2.Range("B1:B17").Cut xlSheet2.Range("E1")
> xlSheet1.Range("B1:B17").Copy ' use copy and then cut if needed
> xlSheet1.Paste xlSheet1.Range("E1")
> xlSheet2.Paste xlSheet2.Range("B1")
> xlSheet2.Range("E1:E17").Copy xlSheet1.Range("B1")
>
> Application.CutCopyMode = False
> End Sub
>
> --
> Hope this helps
> Martin Fishlock
> Please do not forget to rate this reply.
>
>
> "Kc-Mass" wrote:
>
>> Hi
>>
>> I do most of my work in Access but am now working on a reporting system
>> that
>> is about 2/3rds access and 1/3rd Excel. What I am trying to do is open
>> two
>> Excel files in Excel 2003 and copy a range from workbook1, sheet 1 to
>> workbook2, sheet1.
>>
>> I am getting a failure on the "Paste" operation in the second WorkBook.
>>
>> Any and all ideas appreciated.
>>
>> Here is my non working code so far.
>>
>> Sub ExcelRangeChange()
>> 'Set an instance of Excel and pointers for workbooks and sheets
>> Dim xlApp As Excel.Application
>> Dim xlBook1 As Excel.WorkBook
>> Dim xlBook2 As Excel.WorkBook
>> Dim xlSheet1 As Excel.Worksheet
>> Dim xlSheet2 As Excel.Worksheet
>> Set xlApp = New Excel.Application
>> Set xlBook1 = xlApp.Workbooks.Open("C:\Documents and
>> Settings\Barbara\My
>> Documents\WkBk1.xls")
>> Set xlSheet1 = xlBook1.Worksheets("Sheet1")
>> Set xlBook2 = xlApp.Workbooks.Open("C:\Documents and
>> Settings\Barbara\My
>> Documents\WkBk2.xls")
>> Set xlSheet2 = xlBook2.Worksheets("Sheet1")
>> xlApp.Visible = True
>>
>> xlSheet2.Range("B1:B17").Select
>> xlSheet2.Range("b1:b17").Cut
>> xlSheet2.Range("E1").Select
>> xlSheet2.Paste
>> Workbooks("wkBk1.xls").Activate
>> Worksheets("Sheet1").Activate
>> xlSheet1.Range("B1:B17").Select
>> xlSheet1.Range("B1:B17").Cut
>> xlSheet1.Range("E1").Select
>> ActiveSheet.Paste
>>
>> Workbooks("WkBk2.xls").Activate
>> Worksheets("Sheet1").Activate
>> xlSheet2.Range("b1").Select
>> ActiveSheet.Paste
>> xlSheet2.Range("e1:e17").Select
>> xlSheet2.Copy
>> Workbooks("WkBk1.xls").Activate
>> xlSheet1.Range("b1").Select
>> ActiveSheet.Paste
>>
>> Kevin C
>>
>>
>>



 
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
How to copy a range of cells and paste into a new workbook in differentcolumns Mas Microsoft Excel Misc 3 16th Aug 2011 02:57 PM
Copy Range, Paste Value in New Workbook BaggieDan Microsoft Excel Programming 2 21st Oct 2009 02:32 PM
Column range to search from a source workbook and then copy/paste DanS Microsoft Excel Programming 0 10th Feb 2009 11:29 PM
Need a macro to copy a range in one workbook and paste into another workbook Paul Microsoft Excel Programming 8 1st Jul 2004 07:42 AM
Copy a range of cells in an unopened workbook and paste it to the current workbook topstar Microsoft Excel Programming 3 24th Jun 2004 12:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:11 AM.