PC Review


Reply
Thread Tools Rate Thread

Changing cell formula in VBA

 
 
affordsol
Guest
Posts: n/a
 
      29th Nov 2007
Hi to all !!


I have a small problem in Excel97, although I don't know if the Excel
version is relevant.

In some of my sheets, I've elaborated some complex formulas which,
sometimes, can lead to
the infamous "#NA" result.

I want to replace the "#NA" by a clean 2 characters "NA" string.

As there are MANY formulas, I want to develop a VBA sub which does the work :

a) pick up the actual formula (which we'll define as FormulaContent)
(maybe save the activecell.address so as to be able to write to
that range)

b) replace it by
IF(ISERROR( FC ),"NA", FC)

WHERE FC is FormulaContent without the preceeding "=" sign


I thank you by advance.
Best regards,

--
Herve Hanuise
 
Reply With Quote
 
 
 
 
carlo
Guest
Posts: n/a
 
      29th Nov 2007
Hi Herve

you could try something like this

Sub Exchange_Formula()

Dim cell_ As Range

Dim str_formula As String
For Each cell_ In ActiveSheet.Cells
If cell_.HasFormula Then
str_formula = Right(cell_.Formula, Len(cell_.Formula) - 1)
cell_.Formula = "=if(iserror(" & str_formula & "),""NA""," &
str_formula & ")"
End If
Next cell_

End Sub


hth

Carlo

On Nov 29, 3:24 pm, affordsol <afford...@discussions.microsoft.com>
wrote:
> Hi to all !!
>
> I have a small problem in Excel97, although I don't know if the Excel
> version is relevant.
>
> In some of my sheets, I've elaborated some complex formulas which,
> sometimes, can lead to
> the infamous "#NA" result.
>
> I want to replace the "#NA" by a clean 2 characters "NA" string.
>
> As there are MANY formulas, I want to develop a VBA sub which does the work :
>
> a) pick up the actual formula (which we'll define as FormulaContent)
> (maybe save the activecell.address so as to be able to write to
> that range)
>
> b) replace it by
> IF(ISERROR( FC ),"NA", FC)
>
> WHERE FC is FormulaContent without the preceeding "=" sign
>
> I thank you by advance.
> Best regards,
>
> --
> Herve Hanuise


 
Reply With Quote
 
affordsol
Guest
Posts: n/a
 
      4th Dec 2007
Hi Carlo !


So sorry for the late reply but I wish to thank you for your code : it does
the job quite perfectly.

I just changed it a little bit to match MY OWN ranges and I post it here so
as to 'empower' anyone which might be interested in the post.

Best regards from Belgium,
Hervé+

'===============================================================
Sub VoidNAinPrintout()
Dim cell_ As Range
Dim str_formula As String
'Here is the all sheet startup
'For Each cell_ In ActiveSheet.Cells
For Each cell_ In ActiveSheet.Range("L25:P26")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J31:U33")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J35:U39")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J41:U44")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J49:U61")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J63:U70")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J75:U77")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J79:U98")
GoSub CLEANME
Next cell_
For Each cell_ In ActiveSheet.Range("J102:U104")
GoSub CLEANME
Next cell_
Exit Sub
'<<<<<<<<<<<<<<<<<<<
CLEANME:
If cell_.HasFormula Then
str_formula = Right(cell_.Formula, Len(cell_.Formula) - 1)
cell_.Formula = "=if(iserror(" & str_formula & "),""NA""," & str_formula
& ")"
End If
Return
'>>>>>>>>>>>>>>>>>
End Sub
'===============================================================

--
Herve Hanuise


"carlo" wrote:

> Hi Herve
>
> you could try something like this
>
> Sub Exchange_Formula()
>
> Dim cell_ As Range
>
> Dim str_formula As String
> For Each cell_ In ActiveSheet.Cells
> If cell_.HasFormula Then
> str_formula = Right(cell_.Formula, Len(cell_.Formula) - 1)
> cell_.Formula = "=if(iserror(" & str_formula & "),""NA""," &
> str_formula & ")"
> End If
> Next cell_
>
> End Sub
>
>
> hth
>
> Carlo
>
> On Nov 29, 3:24 pm, affordsol <afford...@discussions.microsoft.com>
> wrote:
> > Hi to all !!
> >
> > I have a small problem in Excel97, although I don't know if the Excel
> > version is relevant.
> >
> > In some of my sheets, I've elaborated some complex formulas which,
> > sometimes, can lead to
> > the infamous "#NA" result.
> >
> > I want to replace the "#NA" by a clean 2 characters "NA" string.
> >
> > As there are MANY formulas, I want to develop a VBA sub which does the work :
> >
> > a) pick up the actual formula (which we'll define as FormulaContent)
> > (maybe save the activecell.address so as to be able to write to
> > that range)
> >
> > b) replace it by
> > IF(ISERROR( FC ),"NA", FC)
> >
> > WHERE FC is FormulaContent without the preceeding "=" sign
> >
> > I thank you by advance.
> > Best regards,
> >
> > --
> > Herve Hanuise

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with changing cell no in formula SS Microsoft Excel Discussion 2 29th Jul 2010 07:29 PM
changing cell formula specialc Microsoft Excel Worksheet Functions 1 30th Mar 2009 06:59 PM
how to edit formula without changing formula of each cell =?Utf-8?B?c2FkYXQ=?= Microsoft Excel Worksheet Functions 2 24th Apr 2007 02:02 PM
get the value of a formula in cell B1 into cell A1 without changing my current cell selection News Microsoft Excel Programming 3 21st Oct 2006 12:24 AM
Changing a cell reference within a formula w/another cell tx12345 Microsoft Excel Worksheet Functions 5 7th Mar 2006 01:52 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:13 PM.