Consulting

Results 1 to 8 of 8

Thread: Solved: DISPLAY "0" INSTEAD of #NUM!

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Solved: DISPLAY "0" INSTEAD of #NUM!

    I have used a formula =VLOOKUP(F5,$AX$35:$BB$81,5,0) but IF LookUp value is not available in the table, result is #NUM!.. Is it possible not to show #NUM! and instead display NULL or "0" BY using formula or VBA?


    [vba]
    Dim myRange As Range
    Set myRange = Worksheets("TOP PERFORMERS").Range("C4:F50")

    If IsError(myRange.Value) Then
    myRange.Value = "0"

    End If
    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    The result should be #N/A if the value is not found. To correct it you can use:
    =IF(ISERROR(VLOOKUP(F5,$AX$35:$BB$81,5,0)),0,VLOOKUP(F5,$AX$35:$BB$81,5,0))

    This will return 0 even if the lookup value is found but the returned value is actually an error (e.g. there's a #VALUE error in the data)
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    That formula works perfect... Thanks... But can we make it in VBA?
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  4. #4
    Jammer,
    Something that helped me was to do these in the macro recorder.
    One of the members showed me, that if you remove the "=" in the formula, start the recorder, place the "=" in front of the formula, stop the recorder. You will have the syntax for VBA.
    You may need to tweak the A1 notation from R1C1....
    my site: www.ecboardco.com
    was built w/ a majority of the assistance from the board members here... thanks VBAX.

    Just because I see something, doesn't mean that what's actually happening is what I see.

    You don't get from 0-90 by standing still!

  5. #5
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You asked for formula or VBA. Formula is better as it is dynamic; if you use VBA you have to run it every time the formulas recalculate to ensure the data is up to date and you also need to worry about replacing the formulas that you deleted in case the data has been changed in your data source.
    Regards,
    Rory

    Microsoft MVP - Excel

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Not efficient, but ...

    This is workbook event code.
    To input this code, right click on the Excel icon on the worksheet
    (or next to the File menu if you maximise your workbooks),
    select View Code from the menu, and paste the code

    [vba]

    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Dim rtn
    If Sh.Name = "TOP PERFORMERS" Then
    On Error Resume Next
    rtn = Application.VLookup(Sh.Range("F5").Value, Sh.Range("AX35:BB81"), 5, 0)
    On Error GoTo 0
    If IsError(rtn) Then rtn = 0
    Sh.Range("H1").Value = rtn '<===change H1 to your target cell
    End If
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Thanks to all your reply & advice.
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Can I ask why you use the On Error Resume Next and On Error Goto 0 there? Habit, or in case behaviour changes in a different version?
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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