VBA: Run-time Error 1004: PasteSpecial

th3h0bb5

New Member
Joined
Oct 16, 2015
Messages
10
Hello wonderful Excel gurus,

I have a bit of VBA code I could use help with. It's fairly straight-forward. Based on two cells, tt looks up a value in another workbook, copies it, closes that workbook, and pastes the value where I want it. However, I'm getting a 1004 error.

Here's the code:

Sub copytimezone()


Dim client As String
Dim Domain As String


client = Worksheets("Dashboard").Range("$BV$1").Value
Domain = Worksheets("Dashboard").Range("$BV$2").Value

Worksheets("Dashboard").Activate
Worksheets("Dashboard").Range("BW2").Clear
Workbooks.Open Filename:="XXXXXXX"& client & "&Domain=" & Domain & "&rs:Format=CSV"


' Select the range to copy from the csv
Range("A2").Select
Selection.Copy


'Selection.ClearContents
Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=False


' Note:- Name of this file should be same as the file name.
Worksheets("Dashboard").Activate


'Copy csv data into this spread sheet at this Range
Application.ScreenUpdating = True
Worksheets("Dashboard").Range("BW2").Select
Worksheets("Dashboard").Range("BW2").PasteSpecial

The line is red is where the debugger points. After reading other threads, here's what I've tried so far:

- Putting a pause before the PasteSpecial
- Using just Paste instead of "PasteSpecial"
- Using Active Worksheet instead of specifying the worksheet

None of this has helped. Any help you can give would be glorious.

Cheers!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
try selection.paste or selection.pastespecial
 
Upvote 0
Try clicking on "Record Macro", do it manually and see what code excel writes for you
I walked through your code on my machine and it works.

Either we are both missing something, or there may be some file corruption with one of the files you are using.
 
Upvote 0
Oddly, the error only occurs part of the time, which adds to my confusion. Probably 1 in 5 times, it will run just fine.

Here's the code from performing the process manually:

Windows("XXXXXX.csv").Activate
Range("A2").Select
Selection.Copy
ActiveWindow.Close
Range("BW2").Select
ActiveSheet.Paste

This code is essentially the same thing as what I was working with, just a bit simpler. I tried replacing my code with this, and the error persists.
 
Upvote 0
That sounds like there's some corruption.

Try this:

Copy your code into notepad or notepad++ if you have it.
Delete the code module that is blowing up
Add a new code module to your project
Copy the code from notepad into the new project.


The same thing happened to me about a month ago. That fixed it for me.
 
Upvote 0
did you try?

Code:
Windows("XXXXXX.csv").Activate
Range("A2").Select
Selection.Copy
ActiveWindow.Close
[COLOR=#ff0000]Worksheets("Dashboard").Activate[/COLOR]
Range("BW2").Select
ActiveSheet.Paste

It's your last code with a piece of the former

Bye
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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