Storing values in a name

Kendo

New Member
Joined
Jul 11, 2012
Messages
20
I am using the following code to store a formula in a range name. Is there a way to store the value derived from that formula in another name so that the next time the macro is run it can compare the old value to the current value.

Names.Add name:="TotalExp", rrefersTo:="=Sum(Sheet1!$D:$D)"

Any suggestions would be appreciated.
 

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.
Hello again,
I seem to be missing something.
I used your code to get the correct value which I can insert into a worksheet but I cannot do any calculations within VBA; for example I want to deduct the value in "TotalExp1" from the value in "TotalExp2" But when I attempt to run the macro I get Run time error 13 Type mismatch.
What am I missing?
 
Upvote 0
Hello again,
I seem to be missing something.
I used your code to get the correct value which I can insert into a worksheet but I cannot do any calculations within VBA; for example I want to deduct the value in "TotalExp1" from the value in "TotalExp2" But when I attempt to run the macro I get Run time error 13 Type mismatch.
What am I missing?

Show us the code you tried to use that produced that error.
 
Upvote 0
My code is:


Names.add Name:="NewTotExp1", RefersTo:=WorksheetFunction.Sum(Sheet1.Range("D:D"))
CurrExp = Names("NewTotExp1").Value
PrevExp = Names("TotalExp1").Value

Mydiff = CurrExp - PrevExp
 
Upvote 0
My code is:


Names.add Name:="NewTotExp1", RefersTo:=WorksheetFunction.Sum(Sheet1.Range("D:D"))
CurrExp = Names("NewTotExp1").Value
PrevExp = Names("TotalExp1").Value

Mydiff = CurrExp - PrevExp
How did TotalExp1 get created? Is it a cell reference or a calculated value (like you did with NewTotExp1)?
 
Upvote 0
Is this resolved Kendo???

Try F8 instead of F5. This will execute the code line by line. And it will be easy to debug what is wrong with the code.
You can try to add 'quick watch' for PrevExp variable and get some clue about the error.
 
Upvote 0
No I have not resolved the problem.
I am getting a run time error 13 Type Mismatch at the line MyDff = CurrExp - PevExp
When I hover over PrevExp it shows the value as "=1152" Likewise with CurrExp it shows the value as "=2392"
Do the quotes around the values indicate that they are of type string.
If s How do I convert them to integer?
 
Upvote 0

Forum statistics

Threads
1,203,078
Messages
6,053,403
Members
444,662
Latest member
AaronPMH

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