Copying from one workbook to another VBA paste values only

orangebloss

Board Regular
Joined
Jun 5, 2013
Messages
51
Office Version
  1. 365
Platform
  1. Windows
I have 2 workbooks, DTR Index and DTR Template.

When the DTR index is completed I need to open the DTR template and paste the values from one range to another, then save as a new file name.

I was partially there but it doesn't seem to want to work - I get a Run time error 91: Object variable or with block variable not set

Code:
Sub Sample()
Dim DTRINDEX As Workbook
Dim DTRTEMPLATE As Workbook
Dim path As String
Dim filename As String
path = "H:\Peter Test\"
filename = Range("F3")


Set DTRTEMPLATE = Workbooks.Open("T:\Server\Group\Two\PM\Peter\DTR_Template.xlsx")

'Copy the range to the new worksheet
DTRTEMPLATE.Sheets("DTR Form").Range("B2:F13").copy
'Paste special values to new worksheet
DTRINDEX.Sheets("DTR Form").Range("B2:F13").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
'Save the workbook with specified value as name
ActiveWorkbook.SaveAs filename:=path & filename & ".xlsx", FileFormat:=51

End Sub

Any help much appreciated
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You haven't set DTRINDEX therefore it's "Nothing"
 
Upvote 0
Thanks - another go... but now it sticks on the DTRTEMPLATE.Sheets("DTR Form").Range("B2") etc with an automation error?

Code:
Sub Sample()
Dim DTRINDEX As Workbook
Dim DTRTEMPLATE As Workbook
Dim path As String
Dim filename As String
path = "H:\Peter Test\"
filename = Range("F3")
Set DTRINDEX = ActiveWorkbook

DTRINDEX.Sheets("DTR Form").Range("B2:F13").copy
Set DTRTEMPLATE = Workbooks.Open("T:\Guildford File Server No1\Group\NewProdDev\PMO\Peter\DTR_Template.xlsx")

'DTRINDEX.Range("B2:F13").Value = DTRTEMPLATE.Range("B2:F13").Value

DTRTEMPLATE.Sheets("DTR Form").Range("B2").PasteSpecial Paste:=xlPasteValues
 
DTRTEMPLATE.SaveAs filename:=path & filename & ".xlsx", FileFormat:=51

End Sub
 
Upvote 0
Do you have a sheet called "DTR Form" (check for any leading/trailing spaces) & is that sheet protected?
 
Upvote 0
Are the merged cells in the range B2:F13?
 
Upvote 0
Yup :( current work around is to paste it on a hidden sheet where there are no merged cells and pull it through with formulas. Not ideal but....
 
Upvote 0
Merged cells are a major problem both within xl & VBA.
Best to avoid them like the plague.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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