VBA copy paste between two books. 104 Paste Special method

KirillKU

New Member
Joined
Jul 29, 2014
Messages
4
Hi, I found a number of similar topics, however it did not help my code. Range from opened workbook should be copied into new opened file test and saved.
This is a piece of code:


Set Sheet = ThisWorkbook.Sheets("store data")
Sheet.Range(Cells(i, 4), Cells(i + 365, 19)).Copy
'Set wSource = ActiveWorkbook
Set MasterSource = Workbooks.Open("test.xlsx")
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

paste special row returns an error. Please help
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi
Try replace

Code:
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

by this

Code:
Workbooks("test.xlsx").Sheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

regards
 
Upvote 0
Hi
Try replace

Code:
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

by this

Code:
Workbooks("test.xlsx").Sheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

regards

Sorry, it is out of range now.
 
Upvote 0
Sure,

Dim MasterSource As Workbook
Sheets("Store data").Select
For i = 1820 To 6000
If Sheet.Cells(i, 1).Value = StoreNumber Then
Application.ScreenUpdating = True
Set Sheet = ThisWorkbook.Sheets("store data")

Sheet.Range(Cells(i, 4), Cells(i + 365, 19)).Copy
Set MasterSource = Workbooks.Open("\\140fp-9260\cafegroup$\test.xlsx")
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

' MasterSource.Sheets("store data").Range("D" & i).Value = wSource.Sheets("store data").Range("D" & i&).Value
Application.CutCopyMode = False
MasterSource.Close SaveChanges:=True

Exit For
Else: End If
Next i

Thank you


The error is "1004" PasteSpecial method of range class failed.
In case if I use full workbook name as you adviced, I am getting Error "9" Subscript out of range at the same line
 
Last edited:
Upvote 0
try this.

Code:
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial xlPasteValues

replace by

Code:
MasterSource.Worksheets("store data").Range(Cells(i, 4), Cells(i + 365, 19)).PasteSpecial Paste:=xlPasteValues
 
Upvote 0
Hi, I figured out what VBA did not like in my code. It required sheet name before the cell in this part Range(sheet.Cells(i, 4), sheet.Cells(i + 365, 19)). It works now

Can somebody please what is the best practice to switch between workbooks/sheets properly?
Thank you
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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