Solved

Update a formula

Posted on 2011-02-16
12
263 Views
Last Modified: 2012-05-11
I have a lot of formulas that I need to change. Example below:
The original formula says:
=SUM(B13:B16)

I need it to say:
=IF(SUM(B13:B16)=0,NA(),SUM(B13:B16))

How can I automate adding those sections to the formula? I can't do a drag or copy/paste because there are different sum ranges (the formulas sum 4 or 5 cells, it's not consistent).

I tried using a macro, but that doesn't seem to work because it bases the formula off the current cell reference, instead of what is currently in the cell formula.  

Can you help? Let me know if you need more info.
Thanks,
Elizabeth
0
Comment
Question by:AnalyticsTeam
  • 6
  • 4
  • 2
12 Comments
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 34908542
macro...
select a cell and run this code...
select the other and run this code... until you finish...

if there's too many cells, let me know, I can try to automate the process in a better way ...
Sub UpdateFormula
dim rng as range
dim strFormula as string
set rng = activecell
strFormula = rng.formula
strFormula = "=IF(" & strFormula & "=0,NA()," & strFormula & ")"
rngFormula = strFormula
set rng = nothing
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 34908554
ops, my bad... use this code instead:

Sub UpdateFormula
dim rng as range
dim strFormula as string
set rng = activecell
strFormula = rng.formula
strFormula = "=IF(" & strFormula & "=0,NA()," & strFormula & ")"
rngFormula.Formula = strFormula
set rng = nothing
End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 150 total points
ID: 34908574
Just to note that Fernando's code could be adapted to loop through all cells with a formula if that were needed. Btw missing a dot in line 7 above.
Sub UpdateFormula()

Dim rng As Range
Dim strFormula As String

For Each rng In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
    strFormula = rng.Formula
    strFormula = "=IF(" & strFormula & "=0,NA()," & strFormula & ")"
    rng.Formula = strFormula
Next rng
Set rng = Nothing

End Sub

Open in new window

0
 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 250 total points
ID: 34908616
Stephen,
Thanks for writing with the loop...

Indeed i had forgot the ".Formula", it happens !  :)
AnalyticsTeam, enjoy ! :-)

p.s: do you know how to set up a macro to run ?
Alt+F11
Ctrl+R
Right-Click / Insert / Module
Paste the code
With the cursos anywhere within the new code, hit F5
0
 

Author Comment

by:AnalyticsTeam
ID: 34909132
Thank you guys!

I don't want it to run through the entire worksheet because I have other formulas that need to stay the same.

What do I need to change in line 7 for it to work?
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 150 total points
ID: 34909180
If you just want one cell at a time, you need this:

rng.Formula = strFormula

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 34909246
if you want one cell at a time, use the code on post 34908554, because it has the change on line 7 already :) and it works only on the active cell !
0
 

Author Comment

by:AnalyticsTeam
ID: 34909501
Ugh....I'm so sorry to keep bothering. I did just what you said and when I run the macro I get

"Run-time error '1004':

Application-defined or object-defined error


I see the range is set as the active cell, but it's not working? Any more advice? I've attached a file with an example.
Example-Formula-Change.xlsm
0
 
LVL 6

Assisted Solution

by:FernandoFernandes
FernandoFernandes earned 250 total points
ID: 34909606
sorry, it was my bad.... Stephen tried to warn me and I didnt give proper attention.

this is the right code:
Sub UpdateFormula()
Dim rng As Range
Dim strFormula As String
Set rng = ActiveCell
strFormula = VBA.Right(rng.Formula, VBA.Len(rng.Formula) - 1)
strFormula = "=IF(" & strFormula & "=0,NA()," & strFormula & ")"
rng.Formula = strFormula
Set rng = Nothing
End Sub

Open in new window

0
 
LVL 6

Expert Comment

by:FernandoFernandes
ID: 34909625
there were two problems:
1) rngFormula = strFormula was changed to rng.Formula = strFormula
2) when we take the original formula, we have to remove the "=" from its begining before applying to the IF() and put it inside the cell... none of us had thought of it, because we actually forgot :) It happens ...

please let us know if everything is ok now !
0
 

Author Comment

by:AnalyticsTeam
ID: 34909652
Ok that is cool. It works, thank you both so very much!
0
 

Author Closing Comment

by:AnalyticsTeam
ID: 34909669
Great response times, thanks for helping it see it all the way through - I appreciate it guys!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now