PC Review


Reply
Thread Tools Rate Thread

Copy & Paste - Unsafe?

 
 
Nick H
Guest
Posts: n/a
 
      18th Aug 2009
Hi, I'm hoping that an MVP can help settle a dispute (and save me some
reprogramming).

I have written a consolidation routine that takes data from a number
of formatted workbooks and consolidates it into a single 'Master_Data'
sheet.

As the code loops through each source workbook it defines the area to
be copied then copies and pastes it to the Master_Data sheet using the
following 2 lines of code...

rngAllocation.Copy

rngMaster.PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False

During a code review, a colleague pointed out that this was unsafe
because another Excel application could be running at the same time on
the dual processor server and use the Windows paste buffer between my
code's copy and paste buffer operations, causing my code to paste the
wrong information.

I've never heard of this happening. Is there a slight risk?

Best regards, Nick H
 
Reply With Quote
 
 
 
 
john
Guest
Posts: n/a
 
      18th Aug 2009
Sorry, I can’t answer that question because it is not something that I have
ever experienced.

You can reduce the possibility of such an occurrence by adding:

Application.CutCopyMode=False to Clear The Clipboard after pastespecial
instruction.

Better still, try and avoid using copy & thus by-pass using the clipboard
altogether using an approach like following:

ringmaster.value = rngAllocation.Value

--
jb


"Nick H" wrote:

> Hi, I'm hoping that an MVP can help settle a dispute (and save me some
> reprogramming).
>
> I have written a consolidation routine that takes data from a number
> of formatted workbooks and consolidates it into a single 'Master_Data'
> sheet.
>
> As the code loops through each source workbook it defines the area to
> be copied then copies and pastes it to the Master_Data sheet using the
> following 2 lines of code...
>
> rngAllocation.Copy
>
> rngMaster.PasteSpecial Paste:=xlPasteValues, _
> Operation:=xlNone, SkipBlanks:=False, Transpose:=False
>
> During a code review, a colleague pointed out that this was unsafe
> because another Excel application could be running at the same time on
> the dual processor server and use the Windows paste buffer between my
> code's copy and paste buffer operations, causing my code to paste the
> wrong information.
>
> I've never heard of this happening. Is there a slight risk?
>
> Best regards, Nick H
>

 
Reply With Quote
 
Nick H
Guest
Posts: n/a
 
      18th Aug 2009
I'd still be interested in an answer to the original question, if
anyone else can offer some insight?

However...

---snip---
> rngmaster.value = rngAllocation.Value

---snip---

....thanks jb that does the job - and I'm kicking myself for not
thinking of it, having gone to all the trouble of defining both ranges
and making them the same size. Doh! Too much pressure to think
straight I guess

btw, if anyone else decides this is the method for them please note
that if your source range has multiple areas (as mine does actually)
then the destination range must have the same number of areas and they
must be the same size as the source areas. You then need to loop
through each area and equate them separately. Like so...

For i = 1 To rngAllocation.Areas.Count
rngMaster.Areas(i).Value = rngAllocation.Areas(i).Value
Next i

....the same goes for copy & paste but I was trying to keep the
original question simple. ;^)
 
Reply With Quote
 
john
Guest
Posts: n/a
 
      18th Aug 2009
Nick,
I pressed post before completing my earlier response - I was going to
mention that ranges should be same size but you have already covered. I alos
was going to suggest that you can use copy where needed like this:

rngAllocation.Copy Destination:=Sheet2.Range("A1")

which should also by-pass the clipboard.

Hope useful


--
jb


"Nick H" wrote:

> I'd still be interested in an answer to the original question, if
> anyone else can offer some insight?
>
> However...
>
> ---snip---
> > rngmaster.value = rngAllocation.Value

> ---snip---
>
> ....thanks jb that does the job - and I'm kicking myself for not
> thinking of it, having gone to all the trouble of defining both ranges
> and making them the same size. Doh! Too much pressure to think
> straight I guess
>
> btw, if anyone else decides this is the method for them please note
> that if your source range has multiple areas (as mine does actually)
> then the destination range must have the same number of areas and they
> must be the same size as the source areas. You then need to loop
> through each area and equate them separately. Like so...
>
> For i = 1 To rngAllocation.Areas.Count
> rngMaster.Areas(i).Value = rngAllocation.Areas(i).Value
> Next i
>
> ....the same goes for copy & paste but I was trying to keep the
> original question simple. ;^)
>

 
Reply With Quote
 
Nick H
Guest
Posts: n/a
 
      18th Aug 2009
On 18 Aug, 17:20, john <j...@discussions.microsoft.com> wrote:
> Nick,
> I pressed post before completing my earlier response - I was going to
> mention that ranges should be same size but you have already covered. I alos
> was going to suggest that you can use copy where needed like this:
>
> rngAllocation.Copy Destination:=Sheet2.Range("A1")
>
> which should also by-pass the clipboard.
>


Except that method won't work with PasteSpecial - Thanks for your help
though John.

Br, Nick H
 
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
Copy/Paste from Outlook Task to Word - Priority won't paste Victoria@dig Microsoft Outlook Discussion 2 1st Apr 2010 08:05 PM
Copy and paste image unavailable to paste into outlook document. =?Utf-8?B?Y2hlcnls?= Microsoft Outlook Discussion 0 20th Nov 2006 06:49 PM
How do I get copy/paste to copy/paste text and not the whole page =?Utf-8?B?Q2Fyb2wgSi4=?= Microsoft Word Document Management 1 6th May 2005 09:03 PM
Copy & Paste Object without using the Excel Copy Paste functions =?Utf-8?B?R2Fueg==?= Microsoft Excel New Users 0 10th Mar 2004 07:06 AM
Copy & Paste Object without using the Excel Copy Paste functions =?Utf-8?B?R2Fueg==?= Microsoft Excel Misc 0 10th Mar 2004 07:06 AM


Features
 

Advertising
 

Newsgroups
 


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