?
Solved

Update a formula

Posted on 2011-02-16
12
Medium Priority
?
280 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 600 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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
LVL 6

Accepted Solution

by:
FernandoFernandes earned 1000 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 600 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 1000 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

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Manually copying shapes and their assigned macros one by one to a new location can be tedious, but if you use the Excel utility workbook attached to this article, the process will be much quicker and easier.
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.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

600 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