PC Review


Reply
Thread Tools Rate Thread

Copy/Paste in VBA fails?

 
 
Maury Markowitz
Guest
Posts: n/a
 
      10th Sep 2008
I need to copy down some header text, clear out a spreadsheet, and
then paste the headers back in. I can't get it to work. Anyone see a
problem with...

' copy down the headers we have in the user area
sheet.Range("A3:AZ3").Copy

' clear out anything that used to be in the sheet
sheet.Range("A3:IV5000").ClearContents
sheet.Range("A3:IV5000").ClearComments
sheet.Range("A3:IV5000").Borders.LineStyle = xlNone

' paste the headers back in
sheet.Paste sheet.Range("A3:AZ3")

If the range is empty, the user is presented with an error stating
that the Paste method failed. If it is not empty, they are instead
presented with the "Data on the Clipboard is not the same size and
shape...".

Anyone know how to do this reliably without user intervention?

Maury
 
Reply With Quote
 
 
 
 
JT
Guest
Posts: n/a
 
      10th Sep 2008
Why not leave the header alone and clear out the data below it by changing
the following lines:

sheet.Range("A4:IV5000").ClearContents
sheet.Range("A4:IV5000").ClearComments
sheet.Range("A4:IV5000").Borders.LineStyle = xlNone

--
JT


"Maury Markowitz" wrote:

> I need to copy down some header text, clear out a spreadsheet, and
> then paste the headers back in. I can't get it to work. Anyone see a
> problem with...
>
> ' copy down the headers we have in the user area
> sheet.Range("A3:AZ3").Copy
>
> ' clear out anything that used to be in the sheet
> sheet.Range("A3:IV5000").ClearContents
> sheet.Range("A3:IV5000").ClearComments
> sheet.Range("A3:IV5000").Borders.LineStyle = xlNone
>
> ' paste the headers back in
> sheet.Paste sheet.Range("A3:AZ3")
>
> If the range is empty, the user is presented with an error stating
> that the Paste method failed. If it is not empty, they are instead
> presented with the "Data on the Clipboard is not the same size and
> shape...".
>
> Anyone know how to do this reliably without user intervention?
>
> Maury
>

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      10th Sep 2008
Or you could do this:

sheet.Range("A3:AZ3").Copy
sheet.Cells.Clear
sheet.Range("A3").PasteSpecial Paste:=xlPasteValues

This assumes nothing in rows 1-2. If rows 1 or 2 contain data that
you want to retain, this suggestion fails.

"Maury Markowitz" wrote:

> I need to copy down some header text, clear out a spreadsheet, and
> then paste the headers back in. I can't get it to work. Anyone see a
> problem with...
>
> ' copy down the headers we have in the user area
> sheet.Range("A3:AZ3").Copy
>
> ' clear out anything that used to be in the sheet
> sheet.Range("A3:IV5000").ClearContents
> sheet.Range("A3:IV5000").ClearComments
> sheet.Range("A3:IV5000").Borders.LineStyle = xlNone
>
> ' paste the headers back in
> sheet.Paste sheet.Range("A3:AZ3")
>
> If the range is empty, the user is presented with an error stating
> that the Paste method failed. If it is not empty, they are instead
> presented with the "Data on the Clipboard is not the same size and
> shape...".
>
> Anyone know how to do this reliably without user intervention?
>
> Maury
>

 
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 code fails Ron Dean Microsoft Excel Programming 0 17th Jan 2006 11:49 AM
Filter Copy/Paste Fails - Two Ranges Selected? Craigm Microsoft Excel Programming 4 15th Jul 2005 04:16 PM
copy paste fails: not enough memory Beemer Biker Windows XP Photos 2 18th Dec 2004 01:25 AM
Copy/Paste fails Jeff Nielsen Windows XP Basics 0 13th Nov 2003 09:39 PM
Copy Paste fails after using up/down/left/right arrow Jeff Nielsen Windows XP General 0 13th Nov 2003 07:36 PM


Features
 

Advertising
 

Newsgroups
 


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