VBA Question (copy/paste)

sparkytech

Board Regular
Joined
Mar 6, 2018
Messages
96
Office Version
  1. 365
  2. 2019
I seem to be hitting a roadblock with code that previously worked fine. I have an "Export" button to copy / paste data from a table into another "Export" workbook which is identical, minus some unneeded sheets. I am getting an error on the paste portion of the code, so I created a new blank workbook to test paste the data in, which works fine. Basically, it errors on paste to an identical renamed workbook, but pastes fine to a blank workbook.

Here is my code:

VBA Code:
Sub ExportCleanExcel(control As IRibbonControl)

'Exports the information in the master sheet to a chosen excel file; will just move the data without macros or other extra features

Dim FileToOpen As Variant
Dim DestWkb As Workbook
Dim MasBotRow As Long
Dim MasLasCol As Long

'Turn off screen updates to improve performance
Application.ScreenUpdating = False

'Allows user to select a file to export to using the traditional open file window
FileToOpen = Application.GetOpenFilename("All Excel Files (*.xls?), *.xls?", , "Please export file")

'If the user selects cancel when choosing a file to open FileToOpen is set to FALSE
'Using this we can check if a file was actually selected before continuing
If FileToOpen <> False Then

'Find the bottom row and last column of the table on the master sheet
MasBotRow = Sheet1.Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
MasLasCol = Sheet1.Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

'Open the file that was selected and then set that workbook as DestWkb
Set DestWkb = Application.Workbooks.Open(FileToOpen)

'Copies rows A5 to W to last row and also Z5 to last row on sheet {skips "X:Y"}

Sheet1.Range("A5:W" & MasBotRow).Copy
DestWkb.Sheets(1).Range("A5:W" & MasBotRow).PasteSpecial xlPasteValues

Sheet1.Range("Z5:Z" & MasBotRow).Copy
DestWkb.Sheets(1).Range("X5:X" & MasBotRow).PasteSpecial xlPasteValues

'Turn off alerts to avoid clipboard notices when closing the file
Application.DisplayAlerts = False

'Close the data file
DestWkb.Close True

'Turn back on alerts after closing the file
Application.DisplayAlerts = True

End If

'Turn back on screen updates
Application.ScreenUpdating = True

MsgBox "Export Complete"

End Sub

I get an error "Run-time error '1004' PasteSpecial method of Range class failed" on this line:

VBA Code:
DestWkb.Sheets(1).Range("A5:W" & MasBotRow).PasteSpecial xlPasteValues

Any ideas what is wrong?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Code:
ThisWorkbook.Sheets("Sheet1").Range("A5:W" & MasBotRow).Copy
HTH. Dave
Thanks! Alright, the original error is gone, and then it was "Run-time error 9, Subscript out of Range". I had to change "Sheet1" to "Master" (the name of the sheet 1).

Code:
ThisWorkbook.Sheets("Sheet1").Range("A5:W" & MasBotRow).Copy

Getting closer... now the error is on the
VBA Code:
DestWkb.Sheets(1).Range("A5:W" & MasBotRow).PasteSpecial xlPasteValues
line and is again "Run-time error '1004' PasteSpecial method of Range class failed".
 
Upvote 0
Thanks! Alright, the original error is gone, and then it was "Run-time error 9, Subscript out of Range". I had to change "Sheet1" to "Master" (the name of the sheet 1).

Code:
ThisWorkbook.Sheets("Sheet1").Range("A5:W" & MasBotRow).Copy

Getting closer... now the error is on the
VBA Code:
DestWkb.Sheets(1).Range("A5:W" & MasBotRow).PasteSpecial xlPasteValues
line and is again "Run-time error '1004' PasteSpecial method of Range class failed".
Disregard. Closed everything, reopened, and its now working. Thanks!
 
Upvote 0
You are welcome. Thanks for posting your outcome. Dave
I think I am getting closer to finding the issue, as it happened again. There is nothing worse than *fixing* a problem without knowing why, as it usually rears its ugly head at some time in the future. My "copy" code searches for the bottom row. There are helper cells way down in the sheet that are not part of the table that dont need to be copied. I *think* when the paste occurs, it doesn't know where to paste these. I changed the code to MasBotRow = 2500 and this *seems* to work every time. Thanks again to the code gurus for all the assistance!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
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