PasteSpecial Method of Range Class Failed Error a Different Versio

G

Guest

Below is a snippet of code taken from a larger piece of code.

A get a runtime error with the line under "ActiveSheet.Paste" highlighted in
the debugger:

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

any help? can't figure out how to use similar responses on the discussion
groups to fix... thanks very much.

SteveC

starting code... then


Range("A1:IV65536").Select
Selection.Copy
ThisWorkbook.Activate
Sheets(ShName).Select

ActiveSheet.Paste

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

Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False

Range("A1").Select

code continues...
 
G

Guest

Because you are copying and pasting an entire sheet have you got cell A1
selected in the sheet for the paste otherwise the area will not be large
enough
 
G

Guest

As a side note - it is almost never necessary to select or activate anything

Sub test()
Const ShName As String = "Sheet3"

Workbooks("Book2").Sheets("Sheet1").Cells.Copy
'Or you could copy just the used range
'Workbooks("Book2").Sheets("Sheet1").UsedRange.Copy

With ThisWorkbook.Sheets(ShName).Range("A1")
.PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
skipBlanks:=False, _
Transpose:=False

.PasteSpecial _
Paste:=xlPasteFormats, _
Operation:=xlNone, _
skipBlanks:=False, _
Transpose:=False

.PasteSpecial _
Paste:=xlPasteColumnWidths, _
Operation:=xlNone, _
skipBlanks:=False, _
Transpose:=False
End With

Application.CutCopyMode = False

End Sub
 
G

Guest

JMB that works better -- no error for me now... but I had to put this:

Workbooks("Book2").Sheets("Sheet1").Cells.Copy

before each PasteSpecial code for it to work (otherwise I would only paste
values, not format for some reason).

Separately, any cluue how to copy charts on the original worksheet and paste
into another worksheet as a picture? Right now the macro you provided
doesn't copy and paste charts... thanks very much for your help... SteveC
 
G

Guest

Not sure why you would have to repeat the copy method - it worked okay for me
(except for the xlPasteColumnWidths - I'm using XL2000 which does not support
it. I assume you're using a later version that does). Unless this line is
in the wrong place:
Application.CutCopyMode = False
or there is something else going on in-between that turns off copymode?

I expect your error was just as Ossiemac said, the entire worksheet was
being pasted so it would fail if A1 was not the active cell on the
destination worksheet.

I don't work w/Charts much - so I can't help you much w/that.
 
G

Guest

OssieMac and JMB... thanks very much

JMB said:
Not sure why you would have to repeat the copy method - it worked okay for me
(except for the xlPasteColumnWidths - I'm using XL2000 which does not support
it. I assume you're using a later version that does). Unless this line is
in the wrong place:
Application.CutCopyMode = False
or there is something else going on in-between that turns off copymode?

I expect your error was just as Ossiemac said, the entire worksheet was
being pasted so it would fail if A1 was not the active cell on the
destination worksheet.

I don't work w/Charts much - so I can't help you much w/that.
 
G

Guest

using excel 2003, by the way...

JMB said:
Not sure why you would have to repeat the copy method - it worked okay for me
(except for the xlPasteColumnWidths - I'm using XL2000 which does not support
it. I assume you're using a later version that does). Unless this line is
in the wrong place:
Application.CutCopyMode = False
or there is something else going on in-between that turns off copymode?

I expect your error was just as Ossiemac said, the entire worksheet was
being pasted so it would fail if A1 was not the active cell on the
destination worksheet.

I don't work w/Charts much - so I can't help you much w/that.
 
G

Guest

Hi Steve,

There are 2 methods:-

1. Using VB,

Set CurrentChart = Sheets("Sheet1").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "\temp.gif"
CurrentChart.Export FileName:=Fname, FilterName:="GIF"

Then you can load the GIF file to your sheet.

2. On Excel, after you copy the chart, press Shift first, then goto Edit.
You will see the option Paste Picture

Njoy!
 
T

Tom Ogilvy

or you could do

Sub aBC()
Dim currentchart As ChartObject
Set currentchart = Sheets("Sheet1").ChartObjects(1)
currentchart.CopyPicture
Sheets("Sheet2").Paste

End Sub

--
Regards,
Tom Ogilvy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top