Paste Special - Run-Time Error 1004

Small Paul

Board Regular
Joined
Jun 28, 2018
Messages
118
Hi

I am having difficulty with a Paste Special line in my VBA and cannot work out why. I have searched various websites and tried a load of different things, without success!

The section of code is:

Code:
Worksheets("Required Data").Activate
ActiveCell.Offset(0, 2).Copy
detail = ActiveCell.value
Sheets(detail).Select
ActiveCell.Offset(-2, 1).PasteSpecial xlPasteValues

The correct cell is being copied. The correct worksheet is selected. The 'Offset' selects the right cell.

Unfortunately it gives a Run-time error 1004: PasteSpecial method of Range class failed.

Any suggestions would be appreciated.

Many thanks
Small Paul.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Any sheet/workbook protection or merged cells and do you get the same result with the code below?

What do you believe the activecell is on Sheets(detail)?

Is detail the name of the sheet, if it is it should have quotes i.e. Sheets("detail").
 
Upvote 0
I knew it would be something simple! It was pasting to a merged cell.
Thank you very much Mark858
 
Upvote 0
Glad you resolved it but your code would be more efficient if you didn't have the Selects but to amend the ode then you need to answer my previous question...

What do you believe the activecell is on Sheets(detail)?

and the same question for Worksheets("Required Data")
 
Last edited:
Upvote 0
The "Required Data" sheet is driven by columns A&B. Column A is defined as "detail" and and has a corresponding worksheet (set up via VBA according to criteria for A&B). So, for example, cell A2 has 1 in it. That piece of code looks at the value and goes to the worksheet named 1.

Now I have removed the Merge Cells command from the 1st macro, i can paste correctly. I now just have to work out how to Merge Activecell to column K!
 
Upvote 0
Much as I hate merged cells (better to use center across selection)...

I now just have to work out how to Merge Activecell to column K

Code:
Range(ActiveCell, Cells(ActiveCell.Row, "K")).Merge

or

Code:
Sub xxxx()
    With Range(ActiveCell, Cells(ActiveCell.Row, "K"))
        .HorizontalAlignment = xlCenter
        .Merge
    End With
End Sub

Center across selection option

Code:
Range(ActiveCell, Cells(ActiveCell.Row, "K")).HorizontalAlignment = xlCenterAcrossSelection
 
Last edited:
Upvote 0
Many thanks. That looks tidier than the version I came up with!
Code:
Range(cells(ActiveCell.row, "C"), cells(ActiveCell.row, "K")).MergeCells = True

.HorizontalAlignment = xlLeft
 
Last edited:
Upvote 0
Try the extra option that I put in as my last edit :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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