VBA PasteSpecial causing Run-time error

emojo689

New Member
Joined
Jul 25, 2017
Messages
10
Good Morning,

I have a workbook containing 7 sheets (4 of which are 'project list' working sheets, and 1 a summary sheet 'Gantt'). The gantt sheet has 4 contiguous tables with headers and a gantt chart line for each listed project in column D. It's format is not the same as the working sheets and I need to keep it that way.

What I'm basically doing with the following code is copying certain columns only from each project worksheet and pasting into the summary Gantt sheet under the relevant tables. Everything works perfectly for me except for the PasteSpecial xlPasteValues portion inserted in thedestination piece. If it is included, Ireceive a run-time error. When I don’thave it included, it copies everything over, but of course it copies it’sformatting as well. I just want thevalues copied over so I can retain my formatting in the Gantt sheet.

Like I said, I really need to keep my Gantt sheet formatting– any ideas?


Sub UpdateSummary()

'clinical table for COPY job

Dim clintbl As ListObject, clinsize As Long, lastC As Long
Set clintbl =Worksheets("ClinicalMicro").ListObjects("TClinical")
clinsize =clintbl.ListRows.Count
lastC =clintbl.HeaderRowRange.Offset(clinsize + 1).End(xlUp).Row

'clinical table for PASTE job

Dim tbl As ListObject, tblGC As Long, tblsize As Integer, clinarray AsRange

Set tbl =Worksheets("Gantt").ListObjects("Clinical")
tblGC = tbl.ListRows.Count
tblsize = clinsize - tblGC

If tblGC < clinsize Then
tbl.Range.EntireRow.Offset(2).Resize(tblsize).Insert Shift:=xlDown

End If

WithWorksheets("ClinicalMicro")

Application.Union(.Range("B2:B" & lastC),.Range("D2:D" & lastC), .Range("F2:F" & lastC),.Range("H2:H" & lastC)).Copy _
Destination:=tbl.HeaderRowRange.Offset(1)

EndWith
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
It would help if you showed us the code that doesn't work ;)
 
Upvote 0
Did you use this syntax?

Code:
Application.Union(.Range("B2:B" & lastC), .Range("D2:D" & lastC), .Range("F2:F" & lastC), .Range("H2:H" & lastC)).Copy
tbl.HeaderRowRange.Offset(1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
 
Upvote 0
Thanks for the quick reply, and Sorry.
Yes, that is the syntax I used, except more specifically as:
" Application.Union(.Range("B2:B" & lastC),.Range("D2:D" & lastC), .Range("F2:F" & lastC),.Range("H2:H" & lastC)).Copy _
Destination:=tbl.HeaderRowRange.Offset(1) .PasteSpecial xlPasteValues "

btw: I'm using Windows 7, Office 2010 (unfortunately at the moment)
 
Upvote 0
Thanks for the quick reply, and Sorry.
Yes, that is the syntax I used, except more specifically as:
" Application.Union(.Range("B2:B" & lastC),.Range("D2:D" & lastC), .Range("F2:F" & lastC),.Range("H2:H" & lastC)).Copy _
Destination:=
tbl.HeaderRowRange.Offset(1) .PasteSpecial xlPasteValues "

btw: I'm using Windows 7, Office 2010 (unfortunately at the moment)

That is not the same syntax as I suggested. The red stuff is incorrect for PasteSpecial. Copy all of my code and try it.
 
Upvote 0
Funny....I had what you suggested before I added the "destination" part and it kept throwing an "expected end of statement" so I went with the destination part. Putting the actual destination piece on a separate line worked. Go figure.

Works perfectly!!! :) Thanks a lot for your help
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,124
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top