copy a formula from a cell in one workbook to a cell in another workbook

awd_awd

New Member
Joined
Oct 30, 2014
Messages
11
Hello,

When attempting to get a macro I'm creating (Excel 2010) to copy a formula from a cell in one workbook to a cell in another workbook, it behaves. However when that formula contains a reference to another worksheet, the pasted formula will contain an added reference to the worksheet in the other workbook, as in:

='\\server\Shared\\[workbook.xlsm]EUROPE MASTER'!AB86 - INCORRECT

='EUROPE MASTER'!AB86 - CORRECT

CODE:



Sub openAndCopy()
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range



Set wbCopy = Workbooks.Open("\\server\Shared\\[workbook.xlsm]EUROPE MASTER'!AB86")
Set wsCopy = wbCopy.Worksheets("Pricer")
Set rngCopy = wsCopy.Range("a:ar").EntireColumn
Set wbPaste = Workbooks("AB new pricer.xlsm")
Set wsPaste = wbPaste.Worksheets("Pricer") 'paste to different sheet?
Set rngPaste = wsPaste.Range("a1") 'change this if needed
Application.DisplayAlerts = False
rngCopy.Copy
rngPaste.PasteSpecial
wbCopy.Close
End Sub


Any help appreciated, thanks in advance.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Resize rngPaste so that it is the same size as rngCopy. Then replace:

Rich (BB code):
rngCopy.Copy
rngPaste.PasteSpecial


with:

Rich (BB code):
rngPaste.Formula = rngCopy.Formula
 
Upvote 0
Hi Andrew,

Thanks for your help, unfortunately i now get error 2run-time error '7' out of memory


the debug highlights the new code i replaced as per your advice - rngPaste.Formula = rngCopy.Formula

thanks in advance
 
Upvote 0
Quite strange, i have made the adjustments and still no joy :(

Sub openAndCopy()
Dim wbCopy As Workbook
Dim wsCopy As Worksheet
Dim rngCopy As Range
Dim wbPaste As Workbook
Dim wsPaste As Worksheet
Dim rngPaste As Range






Set wbCopy = Workbooks.Open("\\server\Shared\\[workbook.xlsm]EUROPE MASTER'!AB86")
Set wsCopy = wbCopy.Worksheets("Pricer")
Set rngCopy = wsCopy.Range("a:ar")
Set wbPaste = Workbooks("AB new pricer.xlsm")
Set wsPaste = wbPaste.Worksheets("Pricer")
Set rngPaste = wsPaste.Range("a:ar")
Application.DisplayAlerts = False
rngPaste.Formula = rngCopy.Formula
wbCopy.Close
End Sub

debug highlights

rngPaste.Formula = rngCopy.Formula
 
Upvote 0
This is still entire columns:

Set rngCopy = wsCopy.Range("a:ar")

Be more specific with the range that you want to copy.
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,220
Members
448,876
Latest member
Solitario

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