+ Reply to Thread
Results 1 to 3 of 3

Macro to edit formula

  1. #1
    Registered User
    Join Date
    10-05-2005
    Posts
    2

    Macro to edit formula

    I have a formula which gives a #n/a in the cell :
    =VLOOKUP(A1,B14:C29,2,FALSE)

    In order not to show #n/a, I've edited the cell and changed it to:
    =IF(ISERROR(VLOOKUP(A1,B14:C29,2,FALSE)),0,VLOOKUP(A1,B14:C29,2,FALSE))

    Is there a way to create a macro so that I don't have to edit the formula every time. In other words, the macro will do all the editing for me.

    Thanks in advance for your help!

  2. #2
    Dave Peterson
    Guest

    Re: Macro to edit formula

    One way...

    Option Explicit
    Sub testme()

    Dim myRng As Range
    Dim myCell As Range
    Dim myStr As String

    Set myRng = Nothing
    On Error Resume Next
    Set myRng = Intersect(Selection, _
    Selection.Cells.SpecialCells(xlCellTypeFormulas))
    On Error GoTo 0

    If myRng Is Nothing Then
    MsgBox "No formulas in selection"
    Exit Sub
    End If

    For Each myCell In myRng.Cells
    With myCell
    myStr = Mid(.Formula, 2)
    myStr = "=if(iserror(" & myStr & "),""""," & myStr & ")"
    .Formula = myStr
    End With
    Next myCell

    End Sub

    Select your range of cells to fix and then run this macro.

    Tang123 wrote:
    >
    > I have a formula which gives a #n/a in the cell :
    > =VLOOKUP(A1,B14:C29,2,FALSE)
    >
    > In order not to show #n/a, I've edited the cell and changed it to:
    > =IF(ISERROR(VLOOKUP(A1,B14:C29,2,FALSE)),0,VLOOKUP(A1,B14:C29,2,FALSE))
    >
    > Is there a way to create a macro so that I don't have to edit the
    > formula every time. In other words, the macro will do all the editing
    > for me.
    >
    > Thanks in advance for your help!
    >
    > --
    > Tang123
    > ------------------------------------------------------------------------
    > Tang123's Profile: http://www.excelforum.com/member.php...o&userid=27846
    > View this thread: http://www.excelforum.com/showthread...hreadid=473577


    --

    Dave Peterson

  3. #3
    Registered User
    Join Date
    10-05-2005
    Posts
    2

    Smile

    Thanks Dave.
    It works!
    Much appreciated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1