Solved

Update a formula

Posted on 2011-02-16
12
265 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

776 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