Why the "Range.Formula = " not work?

xxing01

New Member
Joined
May 17, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I want to copy the formula from one cell/cells to another cell/cells by "Range.Formula = ".
But it not work as expected.
If I run the VBA step by step, the function will ended at "Range.Formula = " without error.

VBA Code:
Function test1(sOURCE As Range, tARGET As Range)
    tARGET.Formula = sOURCE.Formula
    test1 = tARGET.Formula
End Function
 

Attachments

  • Range.Formula VBA Copy.PNG
    Range.Formula VBA Copy.PNG
    3.6 KB · Views: 14

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
That's just not how function work.
Unless you are calling a function from code, it is intended to return a value into the same cell. You are wanting it to run like a normal macro.
So in your case it can't work out what you want to return the to cell you are typing it into.
 
Upvote 0
That's just not how function work.
Unless you are calling a function from code, it is intended to return a value into the same cell. You are wanting it to run like a normal macro.
So in your case it can't work out what you want to return the to cell you are typing it into.
Hi Alex,
Can you help me to code the macro, that can copy the formula from any cell to another one?
I want to assign the cell as the function argument.
 
Upvote 0
I don't believe you can do it as a function. You will need to have a button or a short cut key or possibly an Event Macro.
 
Upvote 0
If you want to copy formula from A2 to B2, try with sub, not function
VBA Code:
Sub test()
Set source = Range("A2")
Set target = Range("B2")
target.Formula = source.Formula
End Sub
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: Why the "Range.Formula = " not work?
and Why the "Range.Formula = " not work?

If you have posted the question at more places, please provide links to those as well.


If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,921
Messages
6,127,711
Members
449,399
Latest member
VEVE4014

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