Programmatically retrieving the cell formula expression in terms of custom cell names

DonSnow

New Member
Joined
Apr 28, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am writing the custom cell names and their corresponding formulas to a csv file. In the attached excel sheet, all relevant cells including the formula cells have existing and customized cell names, namely, NewPred_1A, NewPred_1B, NewPred_1C, and NewPred_1D. My goal is to retrieve the expression of the cell location D1(NewPred_1D) in terms of custom names of the other three cells, NewPred_1A, NewPred_1B and NewPred_1C. Currently, the expression for D1(or NewPred_1D) is '=A1+B1+C1' by default even if there are custom names for the cells.

What will I do such that D1 will have an expression of '=NewPred_1A + NewPred_1B + NewPred_1C' without manually updating the expression? How can we assign the formula expression of D1 to a variable? I tried this:

FormulaVar = Worksheets(ActiveSheet.Name).Cells(r, c).Formula but didn't work.
 

Attachments

  • A1.PNG
    A1.PNG
    6.5 KB · Views: 5
  • B1.PNG
    B1.PNG
    8.4 KB · Views: 3
  • C1.PNG
    C1.PNG
    6.6 KB · Views: 2
  • D1 - Wrong.PNG
    D1 - Wrong.PNG
    8.3 KB · Views: 4
  • D1Formula - Correct.PNG
    D1Formula - Correct.PNG
    8.8 KB · Views: 4

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
If your have FormulaVar as String, then
FormulaVar = Worksheets(ActiveSheet.Name).Cells(r, c).Formula
should work just fine as I tested it. At least on my Excel 2016 :unsure:
 
Upvote 0
If your have FormulaVar as String, then
FormulaVar = Worksheets(ActiveSheet.Name).Cells(r, c).Formula
should work just fine as I tested it. At least on my Excel 2016 :unsure:
Thanks for the reply. The formula still showed the '=A1+B1+C1'. How can we tweak the code such that the D1 formula will show the custom names of A1, B1 and C1?

Currently:
1619664704790.png


Goal:
1619664881003.png


Such that if we print the FormulaVar, it should have '=NewPred_1A+NewPred_1B+NewPred_1C' value
 

Attachments

  • Reply1.PNG
    Reply1.PNG
    18.5 KB · Views: 4
  • 1619664645296.png
    1619664645296.png
    5.7 KB · Views: 1
Upvote 0
Thanks for the reply. The formula still showed the '=A1+B1+C1'. How can we tweak the code such that the D1 formula will show the custom names of A1, B1 and C1?

Currently:
View attachment 37722

Goal:
View attachment 37723

Such that if we print the FormulaVar, it should have '=NewPred_1A+NewPred_1B+NewPred_1C' value
Correction: The FormulaVar variable should get the string '=NewPred_1A+NewPred_1B+NewPred_1C' even if the formula box still shows '=A1+B1+C1'
 
Upvote 0
Correction: The FormulaVar variable should get the string '=NewPred_1A+NewPred_1B+NewPred_1C' even if the formula box still shows '=A1+B1+C1'
Did you named the ranges A1, B1, C1 as NewPred_1A, NewPred_1B, NewPred_1C respectively? If you do, when you select A1, the Name Box should show NewPred_1A. If not, it will show A1.
You can use Formula>Name Manager to name each cell. Easier way is to just click A1, then Select the name A1 in the Name Box and type NewPred_1A, So, when you select A1, NewPred_1A will be displayed.
 
Upvote 0
Did you named the ranges A1, B1, C1 as NewPred_1A, NewPred_1B, NewPred_1C respectively? If you do, when you select A1, the Name Box should show NewPred_1A. If not, it will show A1.
You can use Formula>Name Manager to name each cell. Easier way is to just click A1, then Select the name A1 in the Name Box and type NewPred_1A, So, when you select A1, NewPred_1A will be displayed.
Yes I named the ranges A1, B1, C1 as NewPred_1A, NewPred_1B, NewPred_1C respectively. Also the D1 to NewPred_1D. But the formula variable for NewPred_1D still shows
1619667181629.png



1619667063574.png


What code tweaking is needed such that the formula variable will be assigned NewPred_1A + NewPred_1B + NewPred_1C and not A1+B1+C1?
 

Attachments

  • 1619667055181.png
    1619667055181.png
    9 KB · Views: 2
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: Programmatically retrieving the cell formula expression in terms of custom cell names
and How to display the expression in a formula cell by the defined names of reference cells?
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.
 
Upvote 0
Yes I named the ranges A1, B1, C1 as NewPred_1A, NewPred_1B, NewPred_1C respectively. Also the D1 to NewPred_1D. But the formula variable for NewPred_1D still shows
View attachment 37730


View attachment 37729

What code tweaking is needed such that the formula variable will be assigned NewPred_1A + NewPred_1B + NewPred_1C and not A1+B1+C1?
Then I have no idea. I have no Excel 365 but 2016 :confused:
 
Upvote 0

Forum statistics

Threads
1,214,996
Messages
6,122,636
Members
449,092
Latest member
bsb1122

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