Solved

Update a formula

Posted on 2011-02-16
12
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

734 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